我想以编程方式获得事件日志,出现在eventvwr中的一个文件夹中。这个是Windows 8应用程序:Microsoft-Windows-AppHost/Admin。我以Admin的身份运行。
这是不工作:
System.Diagnostics.EventLog.SourceExists("Microsoft-Windows-AppHost/Admin");
这个也不工作:
EventLogQuery queryMicrosoftWindowsAppHost = new EventLogQuery("Microsoft-Windows-AppHost/Admin", PathType.LogName);
_eventsMicrosoftWindowsAppHost = new List<EventRecordWrittenEventArgs>();
_eventLogWatcherMicrosoftWindowsAppHost = new EventLogWatcher(queryMicrosoftWindowsAppHost);
_eventLogWatcherMicrosoftWindowsAppHost.EventRecordWritten += (object sender, EventRecordWrittenEventArgs e) =>
{
_eventsMicrosoftWindowsAppHost.Add(e);
};
_eventLogWatcherMicrosoftWindowsAppHost.Enabled = true;
您可以使用以下代码读取事件日志:
EventLogReader reader = new EventLogReader("Microsoft-Windows-AppHost/Admin");
var evt = reader.ReadEvent();
while (evt!= null)
{
// Write the message to the console
Console.WriteLine(evt.FormatDescription());
evt = reader.ReadEvent();
}
如果您想要附加处理程序,您的代码很接近,但是您可以简单地将字符串传递给监视程序,而不是使用查询对象:
var watcher = new EventLogWatcher("Microsoft-Windows-AppHost/Admin");
watcher.EventRecordWritten += (object s, EventRecordWrittenEventArgs e1) =>
{
Console.WriteLine(e1.EventRecord.FormatDescription());
};
watcher.Enabled = true;