Determines whether an event source is registered on the local computer.
[Visual Basic] Overloads Public Shared Function SourceExists( _ ByVal source As String _ ) As Boolean [C#] public static bool SourceExists( string source ); [C++] public: static bool SourceExists( String* source ); [JScript] public static function SourceExists( source : String ) : Boolean;
true if the event source is registered on the local computer; otherwise, false.
Use this overload to determine if an event source exists on the local computer. If you want to determine whether a log exists on the local computer, use Exists.
This method looks in the registry; if you do not have the appropriate registry rights on the local computer, the query will return false.
Because a new source can't be given the name of an existing source on the same computer, use this method before attempting to call CreateEventSource to ensure that a source with the name specified by source does not already exist on the local computer. The source parameter is not case sensitive.
SourceExists is a static (in Visual Basic Shared) method, so it can be called on the class itself. It is not necessary to instantiate EventLog to call SourceExists.
The following example connects to an event log, "myNewLog", on the local computer. If the source "myNewSource" does not already exist on the computer, it is created. Then, an entry is written to the Log.
Note that the way to create a new event log is through the static (Shared) member CreateEventSource, which creates a Source and registers it to the Log at the time the log is created. Because this is a static (Shared) member of EventLog, it should be called on the class itself rather than on an instance of the class.
Import the System.Diagnostics namespace for this example.
[Visual Basic]
Private Sub ConnectToEventLog() 'Declare a new event log and event log entry. Dim evtLog As EventLog 'Instantiate a new event log. evtLog = New EventLog 'Otherwise, create a new source and log. If Not EventLog.SourceExists("myNewSource") Then 'If MyNewLog does not exist, it will be created EventLog.CreateEventSource("myNewSource", "myNewLog") End If 'Connect the source to the event instance. evtLog.Source = "myNewSource" 'Write an entry to the event log. evtLog.WriteEntry("Event log entry.") End Sub
EventLog Class | EventLog Members | System.Diagnostics Namespace | EventLog.SourceExists Overload List | CreateEventSource | DeleteEventSource | Exists | Source