8.4. Searching Event Log Entries
Problem
Your application might have added many entries to 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 to which you'll add static methods, allowing you to search for entries in an event log based on various criteria. 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 shown in Example 8-3.
Example 8-3. EventSearchLog class
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[] ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access