繁体   English   中英

C#使用EventLog类从evtx文件读取Eventlog

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM