How Do I...Monitor an event log?
Event logging provides a standard, centralized way for you to have your applications
record important software and hardware events. Windows supplies a standard user
interface for viewing the logs (Event Log). Using the common language runtime's EventLog
component, you can easily connect to existing event logs, and receive event notifications when a new entry is written to
the log.
This sample illustrates how to monitor an event log for new entries.
It is a small console application that can be run from a command prompt. The
application takes one command line argument. The argument is the name of
the log that you want to monitor.
Try running the sample as follows:
> LogMonitor.exe Application
Now run the LogWrite.exe sample and write a new entry to the application log. You
will see that the LogMonitor is being notified about the new entry being
written.
In its simplest form, monitoring an event log involves:
- Creating a new instance of an EventLog component and pointing it to a appropriate event log:
String log;
...
EventLog aLog = new EventLog();
aLog.Log = log;
C#
|
- Adding an event handler:
aLog.EntryWritten += new EventLogEventHandler(OnEntryWritten);
C#
|
- Handling the event notification in your event handler:
public static void OnEntryWritten(Object source, EventLogEvent e) {
Console.WriteLine("Written: " + e.Entry.Message);
}
C#
|
Example
VB LogMonitor.exe
[This sample can be found at C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\QuickStart\howto\samples\Services\EventLog\LogMonitor\]
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright 2004 Microsoft Corporation. All rights reserved.
|