Determines whether an event source is registered on a specified computer.
[Visual Basic] Overloads Public Shared Function SourceExists( _ ByVal source As String, _ ByVal machineName As String _ ) As Boolean [C#] public static bool SourceExists( string source, string machineName ); [C++] public: static bool SourceExists( String* source, String* machineName ); [JScript] public static function SourceExists( source : String, machineName : String ) : Boolean;
true if the event source is registered on the given computer; otherwise, false.
Exception Type | Condition |
---|---|
ArgumentException | The machineName parameter is an invalid computer name. |
Use this overload to determine if an event source exists on the computer specified by the machineName parameter. If you want to determine whether a log exists on the specified computer, use Exists.
This method looks in the registry; if you do not have the appropriate registry rights on the given server, 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 computer. The source and machineName parameters are 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 computer "myComputer". 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", "myComputer") Then 'If MyNewLog does not exist, it will be created EventLog.CreateEventSource("myNewSource", "myComputer", "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 | MachineName