[英]C# Read Eventlog from evtx file with EventLog Class
我正在嘗試從System.Diagnostics中讀取帶有EventLog類的存儲的.evtx。 但這不起作用。
無法使用EventLog Class讀取存儲的evtx文件,或者問題出在哪里?
以下是我的代碼
string source = @"S:\test.evtx";
EventLog eventLog = new EventLog();
eventLog.Source = source;
foreach (EventLogEntry log in eventLog.Entries)
{
Console.WriteLine("{0}\n", log.Message);
}
EventLog的Source屬性是指事件查看器中的“應用程序源”,不一定是導出的源文件。
您需要為Source屬性提供應用程序的名稱,而不是文件名。
更新:如果您堅持要從evtx讀取,則EventLogReader類必須是解決方案。
//EVENT LOG READER
string source = @"C:\testev.evtx";
using (var reader = new EventLogReader(source, PathType.FilePath))
{
EventRecord record;
while ((record = reader.ReadEvent()) != null)
{
using (record)
{
Console.WriteLine("{0} {1}: {2}", record.TimeCreated, record.LevelDisplayName, record.FormatDescription());
}
}
}
//EVENT LOG
EventLog eventLog = new EventLog();
eventLog.Source = "ESENT"; //name of an application
foreach (EventLogEntry log in eventLog.Entries)
{
Console.WriteLine("{0}\n", log.Message);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.