
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Watching the Event Log for a Specific Entry
|
461
Entries = EventLogSearch.FindEntryType(Entries, EventLogEntryType.Error);
Entries = EventLogSearch.FindInstanceId(Entries, 7000);
foreach (EventLogEntry Entry in Entries)
{
Console.WriteLine("Message: " + Entry.Message);
Console.WriteLine("InstanceId: " + Entry.InstanceId);
Console.WriteLine("Category: " + Entry.Category);
Console.WriteLine("EntryType: " + Entry.EntryType.ToString( ));
Console.WriteLine("Source: " + Entry.Source);
}
}
Note that this search mechanism can search within only one event log at a time.
To illustrate how searching works, let’s assume that you are using the
FindInstanceID
method to search on the InstanceID. Initially, you would call the FindInstanceID search
method, passing in a collection that implements the
IEnumerable interface, such as the
EventLogEntryCollection collection (that contains all entries in that event log) or an
array of
EventLogEntry objects. The EventLogEntryCollection is returned by the Entries
property of the EventLog class. The FindInstanceID method will return an array of
EventLogEntry objects that match the search criteria (the value passed in to the second
argument of the
FindInstanceID method).
The real power of this searching method design is that the initial search on the
EventLogEntryCollection ...