This documentation is archived and is not being maintained.

Determining 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 EventLog.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.

    ' Visual Basic
    If Not EventLog.SourceExists("MyApp1") Then
        EventLog.CreateEventSource("MyApp1", "Application")
    End If
    
    // C#
    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:

    ' Visual Basic
    If Not EventLog.SourceExists("MyApp1", "myserver") Then
        EventLog.CreateEventSource("MyApp1", "Application", "myserver")
    End If
    
    // C#
    if (!System.Diagnostics.EventLog.SourceExists("MyApp1", "myserver"))
       System.Diagnostics.EventLog.CreateEventSource(
          "MyApp1", "Application", "myserver");
    

See Also

Removing an Event Source | Adding Your Application as a Source of Event Log Entries | Walkthrough: Exploring Event Logs, Event Sources, and Entries

Show: