O'Reilly logo

C# Cookbook by Jay Hilyard, Stephen Teilhet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6.9. Searching Event Log Entries

Problem

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.

Solution

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]; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required