Reading EventLog C# Errors

允我心安 提交于 2020-01-06 03:24:25

问题


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?


回答1:


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 :)



来源:https://stackoverflow.com/questions/2633466/reading-eventlog-c-sharp-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!