Editar

Compartilhar via


EventLog.SourceExists Method

Definition

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

Overloads

SourceExists(String)

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

SourceExists(String, String)

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

SourceExists(String)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs

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

public:
 static bool SourceExists(System::String ^ source);
public static bool SourceExists (string source);
static member SourceExists : string -> bool
Public Shared Function SourceExists (source As String) As Boolean

Parameters

source
String

The name of the event source.

Returns

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

Exceptions

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

Examples

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

#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( "MySource" ) )
   {
      EventLog::CreateEventSource( "MySource", "MyNewLog" );
      Console::WriteLine( "CreatingEventSource" );
   }

   
   // Create an EventLog instance and assign its source.
   EventLog^ myLog = gcnew 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." );
}
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"))
        {
            // An event log source should not be created and immediately used.
            // There is a latency time to enable the source, it should be created
            // prior to executing the application that uses the source.
            // Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatingEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }

        // 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.");
    }
}
Option Explicit
Option Strict
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
End Class

Remarks

Use this method to determine whether 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, a SecurityException will be thrown.

Note

To search for an event source in Windows Vista and later or Windows Server 2003, you must have administrative privileges.

The reason for this requirement is that all event logs, including security, must be searched to determine whether the event source is unique. Starting with Windows Vista, users do not have permission to access the security log; therefore, a SecurityException is thrown.

Starting with Windows Vista, User Account Control (UAC) determines the privileges of a user. If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. By default, you are in the standard user role. To execute the code that accesses performance counters, you must first elevate your privileges from standard user to administrator. You can do this when you start an application by right-clicking the application icon and indicating that you want to run as an administrator.

Note

A service that is executing under the LocalSystem account does not have the privileges required to execute this method. The solution is to check whether the event source exists in the ServiceInstaller, and if it does not exist, to create the source in the installer.

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.

See also

Applies to

SourceExists(String, String)

Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs
Source:
EventLog.cs

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

public:
 static bool SourceExists(System::String ^ source, System::String ^ machineName);
public static bool SourceExists (string source, string machineName);
static member SourceExists : string * string -> bool
Public Shared Function SourceExists (source As String, machineName As String) As Boolean

Parameters

source
String

The name of the event source.

machineName
String

The name the computer on which to look, or "." for the local computer.

Returns

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

Exceptions

machineName is an invalid computer name.

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

Examples

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

#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( "MySource", "MyServer" ) )
   {
      EventLog::CreateEventSource( "MySource", "MyNewLog", "MyServer" );
      Console::WriteLine( "CreatingEventSource" );
   }

   
   // Create an EventLog instance and assign its source.
   EventLog^ myLog = gcnew 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." );
}
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"))
        {
            // An event log source should not be created and immediately used.
            // There is a latency time to enable the source, it should be created
            // prior to executing the application that uses the source.
            // Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("MySource", "MyNewLog", "MyServer");
            Console.WriteLine("CreatingEventSource");
            Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }

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

Remarks

Use this method to determine whether 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, a SecurityException will be thrown.

Note

To search for an event source in Windows Vista and later or Windows Server 2003, you must have administrative privileges.

The reason for this requirement is that all event logs, including security, must be searched to determine whether the event source is unique. Starting with Windows Vista, users do not have permission to access the security log; therefore, a SecurityException is thrown.

Starting with Windows Vista, User Account Control (UAC) determines the privileges of a user. If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. By default, you are in the standard user role. To execute the code that accesses performance counters, you must first elevate your privileges from standard user to administrator. You can do this when you start an application by right-clicking the application icon and indicating that you want to run as an administrator.

Note

A service that is executing under the LocalSystem account does not have the privileges required to execute this method. The solution is to check whether the event source exists in the ServiceInstaller, and if it does not exist, to create the source in the installer.

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.

See also

Applies to