views:

264

answers:

1

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?

A: 

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

Nayan
My entry.Message dump is this:The description for Event ID 0 from source tvNZB cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.If the event originated on another computer, the display information had to be saved with the event.The following information was included with the event: Service stopped successfully.I just realized that the error is probably because I don't have the service currently installed, heh.
Robert
Look for the string "Service stopped successfully" in this dump. You might need to customize the message string. Good luck!
Nayan