2015-06-12 22 views
2
读取事件日志文件

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

+1

我编辑了自己的冠军。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –

回答

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()); 
     } 
    }   
} 
+1

这绝对解决了这个问题。谢谢! 由于[EventRecord](https://msdn.microsoft.com/en-us/library/system.diagnostics.eventing.reader.eventrecord(v = vs.110).aspx)实现了IDisposable,因此不应将其封装在使用? – PonuryCiastkarz

+1

@PonuryCiastkarz我没有注意到。我只是假设它只是一个简单的数据容器。更新了答案。 –