需要一种通过 RecordID 搜索事件日志的方法

问题描述 投票:0回答:1

我正在尝试搜索其中包含事件日志的文件夹,eventpath 具有我想要访问的特定事件日志的路径。我想使用指定的 RecordID 来查找其相关的 FormatDescription 并将其显示在 MessageBox 中。我希望能够使用事件路径来访问每个事件日志,因为我使用 6 个单独的 .evtx 文件,并且需要对所有文件使用此方法。

我找到了这个解决方案,但是当我尝试查询时出现错误。我试图找到解决办法,但似乎它无法满足我的需要。我评论了它在代码中到底发生在哪里。

这是异常:System.Diagnostics.Eventing.Reader.EventLogException:指定的路径无效。

我找不到此代码的修复程序,但如果有人知道修复程序或其他方法来通过 RecordID 搜索事件日志并给出相应的 FormatDescription,我们将不胜感激。

我在 Windows Presentation Foundation 中使用 C#。

public void getDesc(string recordid)
        {

            string eventpath = getEventPath();
       
            //takes off the .evtx of the path
            string result = eventpath.Substring(0, eventpath.Length - 5);
           //result1 is going to be similar to this:
           //C:\Users\MyName\AppData\Local\Temp\randomTempDirectory\additional_files\DiagnosticInfo\WindowsEventLogs\Application

            string sQuery = "*[System/EventRecordID=" + recordid + "]";
           
            var elQuery = new EventLogQuery(result, PathType.LogName, sQuery);
          
            //this is where it errors out
            //error: Specified Channel Path is invalid
            using (var elReader = new System.Diagnostics.Eventing.Reader.EventLogReader(elQuery))
            {

                List<EventRecord> eventList = new List<EventRecord>();
                EventRecord eventInstance = elReader.ReadEvent();
                try
                {
                    while ((eventInstance = elReader.ReadEvent()) != null)
                    {
                        //Access event properties here:
         
                        string formatDescription = eventInstance.FormatDescription();
                        MessageBox.Show(formatDescription);
                    }
                }
                finally
                {
                    if (eventInstance != null)

                        eventInstance.Dispose();

                }
            }
            

        } 
c# wpf event-log
1个回答
0
投票

此处的路径不是您计算机上的物理路径,而是您正在查看“应用程序”、“安全”...等的事件日志类型。

© www.soinside.com 2019 - 2024. All rights reserved.