2013-03-14 64 views
1

我想獲取C#中的「特殊」事件日誌列表,如「Microsoft \ Windows \ Audio \ CaptureMonitor」日誌和所有其他喜歡它的列表。當我使用System.Diagnostics.EventLog.GetEventLogs()時,它們似乎不會被返回。有沒有特殊的方法來獲取所有特殊事件日誌的列表?在C中枚舉特殊事件日誌#

回答

0

您可以使用WevtUtil.exe tool

訪問事件的命令行日誌信息,使用 WevtUtil.exe工具。該工具位於%SystemRoot%\ System32 目錄中。對於WevtUtil.exe工具幫助,請使用wevtutil /?命令。

我想你可能會使用System.Diagnostigs.Process,啓動該工具,然後捕獲並解析控制檯輸出。

using System; 
using System.Diagnostics; 
using System.Linq; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var output = ""; 
     var p = new Process(); 
     var psi = new ProcessStartInfo("wevtutil.exe", "el"); 

     psi.CreateNoWindow = true; 
     psi.RedirectStandardOutput = true; 
     psi.UseShellExecute = false; 
     p.StartInfo = psi; 
     p.Start(); 

     using (var processOutput = p.StandardOutput) 
     { 
      output = processOutput.ReadToEnd(); 
     } 

     p.WaitForExit(); 

     var eventLogs = output 
      .Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries) 
      .ToList(); 

     foreach (var item in eventLogs) 
     { 
      Console.WriteLine(item); 
     } 
    } 
} 

對於閱讀事件日誌,你可以使用相同的方法(例如,呼叫wevtutil qe Microsoft-Windows-Audio/CaptureMonitor /f:text)或System.Diagnostics.Eventing.Reader Namespace。嘗試以下操作:

using System; 
using System.Diagnostics.Eventing.Reader; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     EventLogQuery subscriptionQuery = 
      new EventLogQuery("Microsoft-Windows-Audio/CaptureMonitor", 
       PathType.LogName, "*"); 

     using (EventLogReader logReader = 
      new EventLogReader(subscriptionQuery)) 
     { 
      DisplayEventAndLogInformation(logReader); 
     } 
    } 

    private static void DisplayEventAndLogInformation(EventLogReader logReader) 
    { 
     for (EventRecord eventInstance = logReader.ReadEvent(); 
      null != eventInstance; eventInstance = logReader.ReadEvent()) 
     { 
      Console.WriteLine("--------------------------------------"); 
      Console.WriteLine("Event ID: {0}", eventInstance.Id); 
      Console.WriteLine("Publisher: {0}", eventInstance.ProviderName); 

      try 
      { 
       Console.WriteLine("Description: {0}", 
        eventInstance.FormatDescription()); 
      } 
      catch (EventLogException) 
      { 
       // The event description contains parameters, 
       // and no parameters were passed to the 
       // FormatDescription method, so an exception is thrown. 
      } 

      // Cast the EventRecord object as an EventLogRecord 
      // object to access the EventLogRecord class properties 
      EventLogRecord logRecord = (EventLogRecord)eventInstance; 
      Console.WriteLine("Container Event Log: {0}", 
       logRecord.ContainerLog); 
     } 
    } 
} 

您可能需要調整一點點EventLogQuery構造函數的查詢參數(*)根據您的需要。主題How to: Query for Events顯示了一個實現示例。

+0

我知道如何閱讀的特殊事件日誌,我試圖讓所有的特殊事件日誌列表。我瀏覽過EventLog *類,到目前爲止還沒有提供任何有用的東西。 :( – 2013-03-14 19:10:16