读取 EventLog C# 错误

发布于 2024-08-28 14:00:36 字数 464 浏览 1 评论 0原文

我的 ASP.NET 应用程序中有一段用 C# 编写的代码,它试图读取事件日志,但它返回一个错误。

EventLog aLog = new EventLog();
aLog.Log = "Application";
aLog.MachineName = ".";  // Local machine

foreach (EventLogEntry entry in aLog.Entries)
{
 if (entry.Source.Equals("tvNZB"))
     Label_log.Text += "<p>" + entry.Message;
}

它返回的条目之一是“无法找到源“tvNZB”中事件 ID“0”的描述。本地计算机可能没有必要的注册表信息或消息 DLL 文件来显示该消息,或者您可能没有权限访问它们。以下信息是事件的一部分:“服务已成功启动。”

我只想要“服务已成功启动”。有什么想法吗?

I have this code in my ASP.NET application written in C# that is trying to read the eventlog, but it returns an error.

EventLog aLog = new EventLog();
aLog.Log = "Application";
aLog.MachineName = ".";  // Local machine

foreach (EventLogEntry entry in aLog.Entries)
{
 if (entry.Source.Equals("tvNZB"))
     Label_log.Text += "<p>" + entry.Message;
}

One of the entries it returns is "The description for Event ID '0' in Source 'tvNZB' cannot be found. The local computer may not have the necessary registry information or message DLL files to display the message, or you may not have permission to access them. The following information is part of the event:'Service started successfully.'"

I only want the 'Service started successfully'. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白龙吟 2024-09-04 14:00:36

试试这个:)

        EventLog aLog = new EventLog();
        aLog.Log = "Application";
        aLog.MachineName = ".";  // Local machine

        string message = "\'Service started\'";

        foreach (EventLogEntry entry in aLog.Entries)
        {
            if (entry.Source.Equals("tvNZB")
             && entry.EntryType == EventLogEntryType.Information)
            {
                if (entry.Message.EndsWith(message))
                {
                    Console.Out.WriteLine("> " + entry.Message);
                    //do stuff
                }
            }
        }

它适用于 Win XP 家庭版。该消息在另一个操作系统上可能有所不同。
最好的方法:通过 System.Diagnostics.Trace.Write 转储 entry.Message 并查看确切的消息。

希望一切顺利:)

Try this :)

        EventLog aLog = new EventLog();
        aLog.Log = "Application";
        aLog.MachineName = ".";  // Local machine

        string message = "\'Service started\'";

        foreach (EventLogEntry entry in aLog.Entries)
        {
            if (entry.Source.Equals("tvNZB")
             && entry.EntryType == EventLogEntryType.Information)
            {
                if (entry.Message.EndsWith(message))
                {
                    Console.Out.WriteLine("> " + entry.Message);
                    //do stuff
                }
            }
        }

It works on Win XP home. The message might be different on another OS.
Best way: dump entry.Message by System.Diagnostics.Trace.Write and see the exact message.

Hope it works smoothly :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文