从路径读取事件日志文件

时间:2015-06-12 17:38:39

标签: c# windows logging event-log

我的问题与此问题非常相似How do you open the event log programatically? 除了我记录任何东西。我需要从多个未连接的计算机创建日志条目的数据库。我得到.evtx文件,然后我尝试处理它们。现在我正在从导出的xml文件中执行此操作。但我想跳过xml转换部分。我读过https://msdn.microsoft.com/en-us/library/System.Diagnostics.EventLog.aspx文章,但我找不到我想要的东西。有没有办法做我想要的而不转换为xml?

1 个答案:

答案 0 :(得分:12)

使用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());
        }
    }        
}
相关问题