【问题标题】:Access nested event log in C#在 C# 中访问嵌套事件日志
【发布时间】:2016-10-01 06:51:36
【问题描述】:

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

我已经枚举了系统中的所有日志,使用来自this question 的提示,如下所示:

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# event-log


    【解决方案1】:

    如果您只想阅读事件,可以尝试 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());
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 2017-01-05
      • 2011-03-10
      • 1970-01-01
      相关资源
      最近更新 更多