Configuring 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 EventLog.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.
Note 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.
- If you want your component to be able to receive notification of EntryWritten events, you must set EventLog.EnableRaisingEvents to true. For more information, see Handling the EntryWritten Event.
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 machine name. You do not need to explicitly call the CreateEventSource method because the EventLog.WriteEntry call will register the source with the log if it has not already been registered.
' Visual Basic Dim EventLog1 As New EventLog("Application", "myserver", "newsource") EventLog1.WriteEntry("Test") // C# System.Diagnostics.EventLog EventLog1 = new System.Diagnostics.EventLog("Application", "myserver", "newsource"); EventLog1.WriteEntry("Test");
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.
' Visual Basic If Not EventLog.SourceExists("ExistingSourceString") Then EventLog.CreateEventSource("ExistingSourceString", "Application") End If Dim EventLog1 As New EventLog() EventLog1.Source = "ExistingSourceString" EventLog1.WriteEntry("TestEntry2") // C# 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");Note If you do not set the MachineName property, as shown in these examples, the local machine 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 EventLogEntry Members.
To configure an instance of the EventLog component
- Create an instance of the EventLog component. For more information, see Creating EventLog Component Instances.
- Set the appropriate combination of the Log, MachineName, and Source properties:
If you will be Do this Reading or monitoring event logs Set Log and MachineName Writing entries to a log using a new Source string Set Source, Log, and MachineName Writing entries to a log for an existing Source Set the Source property to the existing source; the other properties will be set automatically
- To enable your component instance to receive notifications of the EntryWritten event, set the EnableRaisingEvents property to true.