How to: Determine If an Event Source Exists
Once you identify a source for a particular event log, this information is stored in the server's registry files until you remove that source. If you try to re-register a source that has already been registered as a valid source for a given log, the system raises a run-time error. You can use the SourceExists method to determine whether or not a particular source has already been registered.
To determine if a source has already been registered
Call the SourceExists method, specifying the source name to query.
The following example shows how to determine if a source using the string MyApp1 is already registered, and to register it with the Application log if it is not.
If Not EventLog.SourceExists("MyApp1") Then EventLog.CreateEventSource("MyApp1", "Application") End If
if (!System.Diagnostics.EventLog.SourceExists("MyApp1")) System.Diagnostics.EventLog.CreateEventSource( "MyApp1", "Application");
To determine if a source is registered on a remote computer, specify the computer name as a second parameter. The following code shows an example:
If Not EventLog.SourceExists("MyApp1", "myserver") Then Dim create As New EventSourceCreationData("MyApp1", "Application") create.MachineName = "myserver" EventLog.CreateEventSource(create) End If
EventSourceCreationData sourceData = new EventSourceCreationData("MyApp1", "Application"); sourceData.MachineName = "myserver"; if (!System.Diagnostics.EventLog.SourceExists("MyApp1", "myserver")) System.Diagnostics.EventLog.CreateEventSource(sourceData);
See Also
Tasks
How to: Remove an Event Source
How to: Add Your Application as a Source of Event Log Entries
Walkthrough: Exploring Event Logs, Event Sources, and Entries