Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中访问嵌套事件日志#_C#_Event Log - Fatal编程技术网

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

C# 在C中访问嵌套事件日志#,c#,event-log,C#,Event Log,我正在尝试使用EventLog获取与打印机相关的日志 我已经使用来自的提示枚举了系统中的所有日志,如下所示: foreach (var e in EventLogSession.GlobalSession.GetLogNames()) { Console.WriteLine(e); } 我得到了所需日志的日志名-MicrosoftWindowsPrintService/Operational 但是,这段代码正在崩溃: var evt = new Even

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

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

foreach (var e in EventLogSession.GlobalSession.GetLogNames()) {
            Console.WriteLine(e);
        }
我得到了所需日志的日志名-
MicrosoftWindowsPrintService/Operational

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

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

我在管理员的指导下运行MSVC 2015

new EventLog("Application");

如何创建自定义嵌套事件日志?如果您只想读取事件,可以尝试使用
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());
 }
 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());
 }