2016-09-30 48 views
1

我正在嘗試使用EventLog獲取與打印機相關的日誌。在C中訪問嵌套的事件日誌#

我列舉系統中所有日誌,使用提示從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"); 

就像一個魅力,我如何創建自定義嵌套事件日誌?

回答

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