Search code examples
c#event-viewer

How to read event viewer log in Windows XP in C#.net


I'm using the below XML Query to read system "Turn on event" from event viewer in last 24 hours.

string query = "<QueryList>" +
                "<Query Id=\"0\" Path=\"System\">" +
                "<Select Path=\"System\">*[System[(EventID=6005) and TimeCreated[timediff(@SystemTime) &lt;= 86400000]]]</Select>" +
                "</Query>" +
                "</QueryList>";

It works well on Windows 7 and above but on Windows XP, I'm getting the below error:

Error:Operation is not supported on this platform

Can someone please help in reading the Turn On event in Windows XP from event viewer in C#.net?


Solution

  • Filtering by XPpath was introduced first with Vista. For XP you would have to use another implementation using EventLog class.

    Example:

    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))
            {
                //write it somewhere
            }
        }
    }