如何使用EventLog类读取外部事件日志文件

时间:2016-09-01 18:50:31

标签: c# event-log

我可以通过这种方式以编程方式读取系统事件日志的应用程序部分:

EventLog aLog;
aLog = new EventLog("Application", ".");

然后可以在属于EventLogEntryCollection类的aLog.Entries中查找事件条目。

现在,让我们说我想要读取从另一个系统导入的另一个事件日志文件(例如一个名为externLog.evtx的日志),而不是默认的系统日志。但我不确定如何将externLog.evtx(及其绝对路径)传递给EventLog创建调用。例如,此调用失败:

 aLog = new EventLog("c:\\temp\\externLog.evtx", "Application", ".");

我已经看到了这样做的建议,例如this one这样做:

Use System.Diagnostics.Eventing.Reader.EventLogReader:

using (var reader = new EventLogReader(@"path\to\log.evtx", PathType.FilePath)){
EventRecord record;
while((record = reader.ReadEvent()) != null)
{
    using (record)
    {
        Console.WriteLine("{0} {1}: {2}", record.TimeCreated, record.LevelDisplayName, record.FormatDescription());
    }
}        

}

但这是将事件读入EventRecord类的对象。有没有办法将这些外部文件读入EventLog类或EventLogEntry或EventLogEntriesCollections的对象?

0 个答案:

没有答案