2012-06-21 32 views
0

我們使用EventLog來記錄異常。有一個後臺線程,一旦事件日誌已滿,程序會將條目轉移到XML文件中,然後清除事件日誌。獲取windows事件日誌文件的位置

這工作正常,但似乎有太多的工作要完成,我認爲最好只複製用於記錄當前應用程序的.evt文件,然後清除事件日誌。

是否有任何方法可以找到文件的位置/路徑,這將在每個Windows操作系統上工作?

suggested使用

Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Services\\EventLog\\" + e.Log); 

但後來我的應用程序日誌名稱沒有一個文件屬性。

回答

2

你現在如何歸檔它們?也許這種方法可以改進以獲得性能。

下面是一個例子。

EventLogSession els = new EventLogSession(); 
els.ExportLogAndMessages("Security",    // Log Name to archive 
         PathType.LogName,  // Type of Log 
         "*",     // Query selecting all events 
         "C:\\archivedLog.evtx", // Exported Log Path 
         false,     // Stop archive if query is invalid 
         CultureInfo.CurrentCulture); 

或者您可以使用ClearLog()方法。

EventLogSession els = new EventLogSession(); 

// Clears all the events and archives them to the .evtx file 
els.ClearLog("System",   // Channel to Clear 
      "c:\\myLog.evtx"); // Backup File Path 

更多信息可以在這裏找到:

Export, Archive, and Clear Event Logs

+0

感謝,這是非常有幫助的。它看起來像ClearLog方法是非常簡單的使用,我注意到你提到的性能,是ClearLog最好的選擇性能方面?這對我來說很重要,因爲性能是造成這種變化的主要原因。 – Mithir

+0

不知道,但我一定會將它與現在的任何實現進行比較。 –