【问题标题】:C# Read Eventlog from evtx file with EventLog ClassC# 使用 EventLog 类从 evtx 文件中读取事件日志
【发布时间】:2018-04-16 08:23:53
【问题描述】:

我正在尝试使用 System.Diagnostics 中的 EventLog 类读取存储的 .evtx。 但它不起作用。

难道不能用 EventLog 类读取存储的 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);
                    }

【问题讨论】:

    标签: c# windows event-log eventlog-source


    【解决方案1】:

    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);
            }
    

    【讨论】:

    • 感谢您的回复。但是如何添加源,因为 evtx 文件位于网络共享上?
    • 我不确定你为什么需要从 evtx 读取。但如果您需要从另一台计算机的事件查看器中读取,则需要提供 MachineName 属性。
    • 不知道你有没有测试过,但是我已经在下面的线程stackoverflow.com/questions/30809133/…找到了这种方式,但问题是,那个record.FormatDescription()每次都返回Null..
    • FormatDescription 获取当前语言环境中的事件消息。您可能需要通过在本地事件查看器中导入来检查 evtx 文件中的事件是否有事件消息
    猜你喜欢
    • 1970-01-01
    • 2013-05-23
    • 2017-05-26
    • 2018-11-23
    • 2015-08-28
    • 2011-03-10
    • 1970-01-01
    • 2012-11-21
    • 2017-08-17
    相关资源
    最近更新 更多