6.9. Searching Event Log Entries


Your application might have produced many entries in the event log. To perform an analysis of how the application operated, how many errors were encountered, and so on, you need to be able to perform a search through all of the entries in an event log. Unfortunately, there are no good built-in search mechanisms for event logs.


You will eventually have to sift through all the entries your application writes to an event log in order to find the entries that allow you to perhaps fix a bug or improve your application’s security system. Unfortunately, there are no good search mechanisms for event logs. This recipe contains an EventLogSearch class, which contains many static methods allowing you to search for entries in an event log based on a criterion. In addition, this search mechanism allows complex searches involving multiple criteria to be performed on an event log at one time. The code for the EventSearchLog class is:

using System; using System.Collections; using System.Diagnostics; public sealed class EventLogSearch { private EventLogSearch( ) {} // Prevent this class from being instantiated. public static EventLogEntry[] FindTimeGeneratedAtOrBefore( IEnumerable logEntries, DateTime timeGeneratedQuery) { ArrayList entries = new ArrayList( ); foreach (EventLogEntry logEntry in logEntries) { if (logEntry.TimeGenerated <= timeGeneratedQuery) { entries.Add(logEntry); } } EventLogEntry[] entriesArray = new EventLogEntry[entries.Count]; ...

Get C# Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.