Search code examples
c#event-log

Access nested event log in C#


I am trying to get printer-related logs using EventLog.

I've enumerated all logs in system, using hint from this question, like that:

foreach (var e in EventLogSession.GlobalSession.GetLogNames()) {
            Console.WriteLine(e);
        }

And I got log name of needed log - Microsoft-Windows-PrintService/Operational.

However, this piece of code is crashing:

var evt = new EventLog("Microsoft-Windows-PrintService/Operational");

with

An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll
Additional information: The event log 'Microsoft-Windows-PrintService/Operational' on computer '.' does not exist.

I am running MSVC 2015 under administrator.

new EventLog("Application");

is working like a charm, how I can create custom nested event log?


Solution

  • If you're only looking to read events you can try the EventReader class:

     var printerServiceQuery = new EventLogQuery("Microsoft-Windows-PrintService/Operational", PathType.LogName);
     var reader = new EventLogReader(printerServiceQuery);
     EventRecord entry = null;
     while( (entry = reader.ReadEvent()) != null)
     {
         Console.WriteLine(entry.FormatDescription());
     }