在c#中访问嵌套事件日志

本文关键字:事件 日志 嵌套 访问 | 更新日期: 2023-09-27 18:09:57

我正在尝试使用EventLog获取打印机相关日志。

我已经列举了系统中的所有日志,使用这个问题的提示,像这样:

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

我得到了需要的日志名称- Microsoft-Windows-PrintService/Operational

但是,这段代码正在崩溃:

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

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.

我在管理员权限下运行MSVC 2015。

new EventLog("Application");

是工作像一个魅力,我如何创建自定义嵌套事件日志?

在c#中访问嵌套事件日志

如果你只想读取事件,你可以尝试EventReader类:

 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());
 }