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) { }
}
精彩评论