
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
450
|
Chapter 8: Diagnostics
Solution
Use the event log built into the Microsoft Windows operating system to record spe-
cific events that occur infrequently. The
AppEvents class shown in Example 8-3 con-
tains all the methods needed to create and use an event log in your application.
Example 8-3. Creating and using an event log
using System;
using System.Diagnostics;
public class AppEvents
{
// Constructors
public AppEvents(string logName) :
this(logName, Process.GetCurrentProcess( ).ProcessName, ".") {}
public AppEvents(string logName, string source) : this(logName, source, ".") {}
public AppEvents(string logName, string source, string machineName)
{
this.logName = logName;
this.source = source;
this.machineName = machineName;
if (!EventLog.SourceExists(source, machineName))
{
EventSourceCreationData sourceData =
new EventSourceCreationData(source, logName);
sourceData.MachineName = machineName;
EventLog.CreateEventSource(sourceData);
}
log = new EventLog(logName, machineName, source);
log.EnableRaisingEvents = true;
}
// Fields
private EventLog log = null;
private string source = "";
private string logName = "";
private string machineName = ".";
// Properties
public string Name
{
get{return (logName);}
}
public string SourceName