This documentation is archived and is not being maintained.

EventLog.SourceExists Method (String, String)

.NET Framework 1.1

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;

Parameters

source
The name of the event source.
machineName
The name the computer on which to look, or an empty string ("") for the local computer.

Return Value

true if the event source is registered on the given computer; otherwise, false.

Exceptions

Exception Type Condition
ArgumentException The machineName parameter is an invalid computer name.

Remarks

Use this method 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.

Because this method accesses the registry, you must have the appropriate registry permissions on the given server; otherwise, the query will return false.

Because you cannot give a new source 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 (Shared in Visual Basic) method, so it can be called on the class itself. It is not necessary to create an instance of EventLog to call SourceExists.

Example

[Visual Basic, C#, C++] The following example writes an entry to an event log, "MyNewLog", on the computer "MyServer", and creates the source "MySource" if the source does not exist.

[Visual Basic, C#, C++] Note   It is not necessary for you to create the event source in your code. If the source you set in the Source property does not exist, the WriteEntry method creates it before writing to the event log. If you do not specify the Log property of the EventLog instance, the log defaults to the Application log.
[Visual Basic] 
Imports System
Imports System.Diagnostics
Imports System.Threading

Class MySample
    Public Shared Sub Main()
        ' Create the source, if it does not already exist.
        If Not EventLog.SourceExists("MySource", "MyServer") Then
            EventLog.CreateEventSource("MySource", "MyNewLog", "MyServer")
            Console.WriteLine("CreatingEventSource")
        End If
        
        ' Create an EventLog instance and assign its source.
        Dim myLog As New EventLog()
        myLog.Source = "MySource"
        
        ' Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.")
        
        Console.WriteLine("Message written to event log.")
    End Sub ' Main
End Class ' MySample

[C#] 
using System;
using System.Diagnostics;
using System.Threading;
              
class MySample{

    public static void Main(){
    
        // Create the source, if it does not already exist.
        if(!EventLog.SourceExists("MySource", "MyServer")){
            EventLog.CreateEventSource("MySource", "MyNewLog", "MyServer");
            Console.WriteLine("CreatingEventSource");
        }
                
        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";
        
        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.");
        
        Console.WriteLine("Message written to event log.");                                                                        
    }
}
   

[C++] 
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;

int main(){

    // Create the source, if it does not already exist.
    if(!EventLog::SourceExists(S"MySource", S"MyServer")){
        EventLog::CreateEventSource(S"MySource", S"MyNewLog", S"MyServer");
        Console::WriteLine(S"CreatingEventSource");
    }

    // Create an EventLog instance and assign its source.
    EventLog* myLog = new EventLog();
    myLog->Source = S"MySource";

    // Write an informational entry to the event log.    
    myLog->WriteEntry(S"Writing to event log.");

    Console::WriteLine(S"Message written to event log.");                                                                        
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

EventLog Class | EventLog Members | System.Diagnostics Namespace | EventLog.SourceExists Overload List | CreateEventSource | DeleteEventSource | Exists | Source | MachineName

Show: