Export (0) Print
Expand All

EventLog.SourceExists Method

Searches a computer's registry for a given event source.

Overload List

Determines whether an event source is registered on the local computer.

[Visual Basic] Overloads Public Shared Function SourceExists(String) As Boolean
[C#] public static bool SourceExists(string);
[C++] public: static bool SourceExists(String*);
[JScript] public static function SourceExists(String) : Boolean;

Determines whether an event source is registered on a specified computer.

[Visual Basic] Overloads Public Shared Function SourceExists(String, String) As Boolean
[C#] public static bool SourceExists(string, string);
[C++] public: static bool SourceExists(String*, String*);
[JScript] public static function SourceExists(String, String) : Boolean;

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, C#, C++] Note   This example shows how to use one of the overloaded versions of SourceExists. For other examples that might be available, see the individual overload topics.
[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.

See Also

EventLog Class | EventLog Members | System.Diagnostics Namespace

Show:
© 2014 Microsoft