How to retrieve event log other than Application category?

。_饼干妹妹 提交于 2019-12-06 08:34:33

The EventLog class only lets you access Windows event logs. You will want to use instead the EventLogReader found in System.Diagnostics.Eventing.Reader namespace.

        EventLogQuery query = new EventLogQuery("Microsoft-Windows-Application Server-Applications/Operational", PathType.LogName, "*");
        EventLogReader reader = new EventLogReader(query);
        EventRecord eventRecord;
        while ((eventRecord = reader.ReadEvent()) != null)
        {
            Console.WriteLine(String.Format("{0} - {1}",
                eventRecord.TimeCreated,
                eventRecord.FormatDescription()));
        }

You may need to use the EventLogReader and EventLogQuery to achieve this.

EventLogReader reader = new EventLogReader("Microsoft-Windows-Application Server-Applications/Operational");
string message = reader.ReadEvent().FormatDescription();

You can use the EventLogQuery to retrieve results in a descending order.

However, I am not too sure, why this does not work with the EventLog. Maybe somebody else can help clarify that.

@MatthewG 's Answer is partly correct. EventLog class allows you to access administrative logs as per definition in MSDN. But they actually display folder names which are found under the registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\

You can verify this by creating a new folder with the a random name and you'll find the random name being displayed when you query using EventLog.GetEventLogs() method. Something, I believe that Microsoft have implemented very poorly.

So you're going to have to use EventLogQuery and EventLogReader classes to read event logs.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!