How to: Configure EventLog Component Instances
There are three major properties involved in configuring an instance of the EventLog component:
The Log property indicates the log with which you want to interact.
The MachineName property indicates the computer on which the log you are working with resides.
The Source property indicates the source string which will be used to identify your component when it writes entries to a log.
The way you configure your EventLog component instance depends on how you are planning to use it:
If you are planning to write entries to a log, you must do one of two things. Either you must register a source using the CreateEventSource method and set the EventLog component's Source property to that same source, or you must set the MachineName, Log, and Source properties for the component.
You can set the Log property on your component directly, or you can set it in code that registers your component as an event source. After registering the component as a source, a connection is already set in the server's registry linking your component to a specific log.
If you are planning to read log entries or monitor a log, you must set the MachineName and Log properties to indicate the log you are watching. You do not need to set the Source property in this case.
The following example shows how to configure your component to allow you to write entries to the Application log. In this case, the source has not already been registered with the log, so you must specify the source string, log, and computer name. You do not need to explicitly call the CreateEventSource method because the WriteEntry call will register the source with the log if it has not already been registered.
If the source has already been registered, you can simply set the Source property and write your entry. For example, the following code shows how to write an entry to a log using a source that has already been registered.
if (!System.Diagnostics.EventLog.SourceExists("ExistingSourceString")) System.Diagnostics.EventLog.CreateEventSource( "ExistingSourceString", "Application"); System.Diagnostics.EventLog eventLog1 = new System.Diagnostics.EventLog(); eventLog1.Source = "ExistingSourceString"; eventLog1.WriteEntry("TestEntry2");
If you do not set the MachineName property, as shown in these examples, the local computer is assumed.
For more information on these properties, see Event Log References.
The entries you retrieve from an event log are categorized by multiple properties. For information on these properties, see EventLog.
To configure an instance of the EventLog component
If you will be
Reading or monitoring event logs
Writing entries to a log using a new Source string
Writing entries to a log for an existing Source
Set the Source property to the existing source; the other properties will be set automatically