EventLog.SourceExists Metoda

Definicja

Wyszukuje rejestr komputera dla danego źródła zdarzeń.

Przeciążenia

SourceExists(String)

Określa, czy źródło zdarzeń jest zarejestrowane na komputerze lokalnym.

SourceExists(String, String)

Określa, czy źródło zdarzeń jest zarejestrowane na określonym komputerze.

SourceExists(String)

Źródło:
EventLog.cs
Źródło:
EventLog.cs
Źródło:
EventLog.cs

Określa, czy źródło zdarzeń jest zarejestrowane na komputerze lokalnym.

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

Parametry

source
String

Nazwa źródła zdarzeń.

Zwraca

true jeśli źródło zdarzeń jest zarejestrowane na komputerze lokalnym; w przeciwnym razie , false.

Wyjątki

source nie można odnaleźć, ale nie można przeszukać niektórych lub wszystkich dzienników zdarzeń.

Przykłady

Poniższy przykład tworzy źródło MySource , jeśli jeszcze nie istnieje, i zapisuje wpis w dzienniku MyNewLogzdarzeń .

#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

Uwagi

Użyj tej metody, aby określić, czy na komputerze lokalnym istnieje źródło zdarzeń. Jeśli chcesz określić, czy dziennik istnieje na komputerze lokalnym, użyj polecenia Exists.

Ponieważ ta metoda uzyskuje dostęp do rejestru, musisz mieć odpowiednie uprawnienia rejestru na komputerze lokalnym; SecurityException w przeciwnym razie zostanie zgłoszony element .

Uwaga

Aby wyszukać źródło zdarzeń w systemie Windows Vista lub nowszym lub Windows Server 2003, musisz mieć uprawnienia administracyjne.

Przyczyną tego wymagania jest to, że wszystkie dzienniki zdarzeń, w tym zabezpieczenia, muszą być przeszukane, aby określić, czy źródło zdarzeń jest unikatowe. Począwszy od systemu Windows Vista, użytkownicy nie mają uprawnień dostępu do dziennika zabezpieczeń; w związku z SecurityException tym zgłaszana jest wartość .

Począwszy od systemu Windows Vista, kontrola konta użytkownika (UAC) określa uprawnienia użytkownika. Jeśli jesteś członkiem wbudowanej grupy Administratorzy, masz przypisane dwa tokeny dostępu w czasie wykonywania: token dostępu użytkownika standardowego i token dostępu administratora. Domyślnie jesteś w roli użytkownika standardowego. Aby wykonać kod, który uzyskuje dostęp do liczników wydajności, musisz najpierw podnieść poziom uprawnień od użytkownika standardowego do administratora. Możesz to zrobić po uruchomieniu aplikacji, klikając prawym przyciskiem myszy ikonę aplikacji i wskazując, że chcesz uruchomić jako administrator.

Uwaga

Usługa wykonywana na LocalSystem koncie nie ma uprawnień wymaganych do wykonania tej metody. Rozwiązaniem jest sprawdzenie, czy źródło zdarzeń istnieje w obiekcie ServiceInstaller, a jeśli nie istnieje, aby utworzyć źródło w instalatorze.

Ponieważ nie można nadać nowemu źródle nazwy istniejącego źródła na tym samym komputerze, użyj tej metody przed podjęciem próby wywołania, CreateEventSource aby upewnić się, że źródło o nazwie określonej przez source nie istnieje jeszcze na komputerze lokalnym. Parametr source nie jest uwzględniany wielkości liter.

Zobacz też

Dotyczy

SourceExists(String, String)

Źródło:
EventLog.cs
Źródło:
EventLog.cs
Źródło:
EventLog.cs

Określa, czy źródło zdarzeń jest zarejestrowane na określonym komputerze.

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

Parametry

source
String

Nazwa źródła zdarzeń.

machineName
String

Nazwa komputera, na którym ma wyglądać, lub "." dla komputera lokalnego.

Zwraca

true jeśli źródło zdarzeń jest zarejestrowane na danym komputerze; w przeciwnym razie , false.

Wyjątki

machineName jest nieprawidłową nazwą komputera.

source nie można odnaleźć, ale nie można przeszukać niektórych lub wszystkich dzienników zdarzeń.

Przykłady

Poniższy przykład tworzy źródło MySource na komputerze MyServeri zapisuje wpis w dzienniku MyNewLogzdarzeń.

#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

Uwagi

Użyj tej metody, aby określić, czy źródło zdarzeń istnieje na komputerze określonym przez machineName parametr . Jeśli chcesz określić, czy dziennik istnieje na określonym komputerze, użyj polecenia Exists.

Ponieważ ta metoda uzyskuje dostęp do rejestru, musisz mieć odpowiednie uprawnienia rejestru na danym serwerze; SecurityException w przeciwnym razie zostanie zgłoszony element .

Uwaga

Aby wyszukać źródło zdarzeń w systemie Windows Vista lub nowszym lub Windows Server 2003, musisz mieć uprawnienia administracyjne.

Przyczyną tego wymagania jest to, że wszystkie dzienniki zdarzeń, w tym zabezpieczenia, muszą być przeszukane, aby określić, czy źródło zdarzeń jest unikatowe. Począwszy od systemu Windows Vista, użytkownicy nie mają uprawnień dostępu do dziennika zabezpieczeń; w związku z SecurityException tym zgłaszana jest wartość .

Począwszy od systemu Windows Vista, kontrola konta użytkownika (UAC) określa uprawnienia użytkownika. Jeśli jesteś członkiem wbudowanej grupy Administratorzy, masz przypisane dwa tokeny dostępu w czasie wykonywania: token dostępu użytkownika standardowego i token dostępu administratora. Domyślnie jesteś w roli użytkownika standardowego. Aby wykonać kod, który uzyskuje dostęp do liczników wydajności, musisz najpierw podnieść poziom uprawnień od użytkownika standardowego do administratora. Możesz to zrobić po uruchomieniu aplikacji, klikając prawym przyciskiem myszy ikonę aplikacji i wskazując, że chcesz uruchomić jako administrator.

Uwaga

Usługa wykonywana na LocalSystem koncie nie ma uprawnień wymaganych do wykonania tej metody. Rozwiązaniem jest sprawdzenie, czy źródło zdarzeń istnieje w obiekcie ServiceInstaller, a jeśli nie istnieje, aby utworzyć źródło w instalatorze.

Ponieważ nie można nadać nowemu źródle nazwy istniejącego źródła na tym samym komputerze, użyj tej metody przed podjęciem próby wywołania CreateEventSource , aby upewnić się, że źródło o nazwie określonej przez source nie istnieje jeszcze na komputerze. Parametry source i machineName nie są uwzględniane wielkości liter.

SourceExistsstatic jest metodą, więc można ją wywołać w samej klasie. Nie jest konieczne utworzenie wystąpienia EventLog wywołania SourceExistsmetody .

Zobacz też

Dotyczy