This documentation is archived and is not being maintained.

EventLog.SourceExists Method (String)

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

Namespace: System.Diagnostics
Assembly: System (in system.dll)

'Declaration
Public Shared Function SourceExists ( _
	source As String _
) As Boolean
'Usage
Dim source As String
Dim returnValue As Boolean

returnValue = EventLog.SourceExists(source)
public static boolean SourceExists (
	String source
)
public static function SourceExists (
	source : String
) : boolean

Parameters

source

The name of the event source.

Return Value

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

Exception typeCondition

SecurityException

source was not found, but some or all of the event logs could not be searched.

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

Because this method accesses the registry, you must have the appropriate registry permissions on the local computer; 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 local computer. The source parameter is not case-sensitive.

The following example creates the source MySource if it does not already exist, and writes an entry to the event log MyNewLog.

Option Explicit
Option Strict
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") Then
            EventLog.CreateEventSource("MySource", "MyNewLog")
            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

import System.*;
import System.Diagnostics.*;
import System.Threading.*;

class MySample
{
    public static void main(String[] args)
    {
        // Create the source, if it does not already exist.
        if (!(EventLog.SourceExists("MySource"))) {
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatingEventSource");
        }

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.set_Source("MySource");

        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.");
        Console.WriteLine("Message written to event log.");
    } //main
} //MySample

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Show: