Ben*_*Ben 3 c# logging event-handling
我正在尝试侦听Microsoft-Windows-NetworkProfile/Operational日志中的事件。我可以使用以下代码收听主要的 Windows 日志,例如应用程序日志:
public static void SubscribeToLogEvents(string logName, EntryWrittenEventHandler customEventHandler)
{
EventLog log = new EventLog();
log.Log = logName;
//when an entry is written to an event log on the local computer, customEventHandler is fired
log.EntryWritten += customEventHandler;
//Set a value indicating EventLog receives
//System.Diagnostics.EventLog.EntryWritten event notifications.
log.EnableRaisingEvents = true;
}
static void EventLogEntryWritten(object sender, EntryWrittenEventArgs currentEvent)
{
var log = (EventLog)sender;
Console.WriteLine("Event Raised: |Log:{0}|Source:{1}|EventID:{2}|", log.LogDisplayName, currentEvent.Entry.Source, currentEvent.Entry.EventID);
}
Run Code Online (Sandbox Code Playgroud)
如果我使用以下内容,我可以实时查看应用程序日志中发生的事件:
SubscribeToLogEvents("Application", OnEntryWritten);
Run Code Online (Sandbox Code Playgroud)
但是,我想要的事件在这里:
我怎样才能收听这个日志?如果我试试这个:
SubscribeToLogEvents("Microsoft-Windows-NetworkProfile/Operational", OnEntryWritten);
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息,提示“未找到日志”。
很晚了,但这是我在寻求答案时找到的第一篇文章。所以对于一开始发现这个的人:
private void AttachWatcher() {
EventLogQuery logQuery = new EventLogQuery("Microsoft-Windows-NetworkProfile/Operational", PathType.LogName, "*[System[(EventID = 10000)]]");
EventLogWatcher logWatcher = new EventLogWatcher(logQuery);
logWatcher.EventRecordWritten += new EventHandler<EventRecordWrittenEventArgs>(EventWritten);
logWatcher.Enabled = true;
}
private void EventWritten(Object obj, EventRecordWrittenEventArgs arg) {
//Do stuff
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1414 次 |
| 最近记录: |