SourceExists Method (String, String)
Collapse the table of content
Expand the table of content

EventLog.SourceExists Method (String, String)

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

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

public static bool SourceExists (
	string source,
	string machineName
)
public static boolean SourceExists (
	String source, 
	String machineName
)
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 "." for the local computer.

Return Value

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

Exception typeCondition

ArgumentException

machineName is an invalid computer name.

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 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 method, so it can be called on the class itself. It is not necessary to create an instance of EventLog to call SourceExists.

The following example creates the source MySource on the computer MyServer, and writes an entry to the event log MyNewLog.

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.");                                                                        
    }
}
   

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", "MyServer"))) {
            EventLog.CreateEventSource("MySource", "MyNewLog", "MyServer");
            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

Community Additions

ADD
Show:
© 2016 Microsoft