How do I specify events deep inside “Applications

2019-05-15 16:24发布

The following code snippet fires an event when an event is logged. The sample code works fine but the log I want to monitor is actually "Applications and Services Logs > Microsoft > Windows > Task Scheduler > Operational".

What do I insert in place of "Application" in the code sample?

...
EventLog myNewLog = new EventLog("Application", ".", "testEventLogEvent");                 

myNewLog.EntryWritten += new EntryWrittenEventHandler(MyOnEntryWritten);
myNewLog.EnableRaisingEvents = true;
...

1条回答
啃猪蹄的小仙女
2楼-- · 2019-05-15 16:31

The log name is Microsoft-Windows-TaskScheduler/Operational but I don't think you can access it using the EventLog class. I think that log is based on Event Tracing for Windows so you need to use the System.Diagnostics.Eventing.Reader namespace to access it.

The Event Log Scenarios page might be useful, in particular the How to: Subscribe to Events in an Event Log article might help you get started.

Update: The How to: Subscribe to Events in an Event Log code worked for me after I changed the log name (I also changed the query to request Level=4)...

EventLogQuery subscriptionQuery = new EventLogQuery(
    "Microsoft-Windows-TaskScheduler/Operational", PathType.LogName, "*[System/Level=4]");
查看更多
登录 后发表回答