开发者

Use custom views (filters) in event log from C#

Newer versions of Windows 开发者_高级运维have the possibilities to define "custom views" (filters) in the event viewer. On servers, there is e.g. a pre-defined custom view "Administrative Events" which filters on important errors and warnings.

Is there a possibility to access these views from C#, i.e. could I iterate all entries in "Administrative Events"?


The following code shows an example of how to use the EventLog and EventLogEntry classes in the System.Diagnostics namespace to access the different event logs in your system.

EventLog[] eventLogs = EventLog.GetEventLogs(System.Environment.MachineName);

foreach (EventLog currentLog in eventLogs)
{
    Console.WriteLine("Log: " + currentLog.Log);

    int counter = 0;
    try
    {
        foreach (EventLogEntry entry in currentLog.Entries)
        {
            if (counter++ >= 10) break;
            Console.WriteLine(entry.Message);
        }
    }
    catch (SecurityException) { }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜