简体   繁体   中英

Reading EventLog C# Errors

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM