EventLog.SourceExists EventLog.SourceExists EventLog.SourceExists EventLog.SourceExists Method

定義

搜尋指定事件來源的電腦的登錄。Searches a computer's registry for a given event source.

多載

SourceExists(String) SourceExists(String) SourceExists(String) SourceExists(String)

判斷事件來源是否登錄於本機電腦上。Determines whether an event source is registered on the local computer.

SourceExists(String, String) SourceExists(String, String) SourceExists(String, String) SourceExists(String, String)

判斷事件來源是否登錄於指定的電腦上。Determines whether an event source is registered on a specified computer.

SourceExists(String) SourceExists(String) SourceExists(String) SourceExists(String)

判斷事件來源是否登錄於本機電腦上。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

參數

source
String String String String

事件來源的名稱。The name of the event source.

傳回

如果事件來源登錄於本機電腦上,則為 true,否則為 falsetrue if the event source is registered on the local computer; otherwise, false.

例外狀況

找不到 source,但無法搜尋部分或全部的事件記錄檔。source was not found, but some or all of the event logs could not be searched.

範例

下列範例會建立來源MySource (如果尚未存在), 並將專案寫入事件記錄MyNewLog檔。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
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

備註

使用這個方法來判斷本機電腦上是否有事件來源。Use this method to determine whether an event source exists on the local computer. 如果您想要判斷本機電腦上是否有記錄存在, 請使用ExistsIf you want to determine whether a log exists on the local computer, use Exists.

因為這個方法會存取登錄, 所以您必須在本機電腦上具有適當的登入權利。否則, SecurityException將會擲回。Because this method accesses the registry, you must have the appropriate registry permissions on the local computer; otherwise, a SecurityException will be thrown.

注意

若要在 Windows Vista 和更新版本或 Windows Server 2003 中搜尋事件來源, 您必須具有系統管理許可權。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. 從 Windows Vista 開始, 使用者沒有存取安全性記錄檔的許可權;因此, SecurityException會擲回。Starting with Windows Vista, users do not have permission to access the security log; therefore, a SecurityException is thrown.

從 Windows Vista 開始, 使用者帳戶控制 (UAC) 會判斷使用者的許可權。Starting with Windows Vista, User Account Control (UAC) determines the privileges of a user. 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。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.

注意

LocalSystem帳戶下執行的服務沒有執行此方法所需的許可權。A service that is executing under the LocalSystem account does not have the privileges required to execute this method. 解決方法是檢查事件來源是否存在於中ServiceInstaller, 如果不存在, 則在安裝程式中建立來源。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.

因為您無法在同一部電腦上將現有來源的名稱提供給新來源, 所以在嘗試呼叫CreateEventSource之前, 請先使用此方法, 以確保本機電腦上不存在具有source指定名稱的來源。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. source參數不區分大小寫。The source parameter is not case-sensitive.

安全性

EventLogPermission
用於寫入電腦上的事件記錄檔資訊。for writing the event log information on the computer. 相關聯的列舉:WriteAssociated enumeration: Write

另請參閱

SourceExists(String, String) SourceExists(String, String) SourceExists(String, String) SourceExists(String, String)

判斷事件來源是否登錄於指定的電腦上。Determines whether an event source is registered on a specified computer.

public:
 static bool SourceExists(System::String ^ source, System::String ^ machineName);
[System.MonoNotSupported("remote machine is not supported")]
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

參數

source
String String String String

事件來源的名稱。The name of the event source.

machineName
String String String String

要在其上進行尋找的電腦的名稱,或者表示本機電腦的 "."。The name the computer on which to look, or "." for the local computer.

傳回

如果事件來源註冊於指定的電腦上,則為 true;否則為 falsetrue if the event source is registered on the given computer; otherwise, false.

例外狀況

machineName 為無效的電腦名稱。machineName is an invalid computer name.

找不到 source,但無法搜尋部分或全部的事件記錄檔。source was not found, but some or all of the event logs could not be searched.

範例

下列範例會在電腦MySource MyServer上建立來源, 並將專案寫入事件記錄MyNewLog檔。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
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

備註

使用這個方法來判斷事件來源是否存在於machineName參數所指定的電腦上。Use this method to determine whether an event source exists on the computer specified by the machineName parameter. 如果您想要判斷記錄檔是否存在於指定的電腦上, Exists請使用。If you want to determine whether a log exists on the specified computer, use Exists.

因為這個方法會存取登錄, 所以您必須在指定的伺服器上擁有適當的登入權利。否則, SecurityException將會擲回。Because this method accesses the registry, you must have the appropriate registry permissions on the given server; otherwise, a SecurityException will be thrown.

注意

若要在 Windows Vista 和更新版本或 Windows Server 2003 中搜尋事件來源, 您必須具有系統管理許可權。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. 從 Windows Vista 開始, 使用者沒有存取安全性記錄檔的許可權;因此, SecurityException會擲回。Starting with Windows Vista, users do not have permission to access the security log; therefore, a SecurityException is thrown.

從 Windows Vista 開始, 使用者帳戶控制 (UAC) 會判斷使用者的許可權。Starting with Windows Vista, User Account Control (UAC) determines the privileges of a user. 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。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.

注意

LocalSystem帳戶下執行的服務沒有執行此方法所需的許可權。A service that is executing under the LocalSystem account does not have the privileges required to execute this method. 解決方法是檢查事件來源是否存在於中ServiceInstaller, 如果不存在, 則在安裝程式中建立來源。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.

因為您無法在同一部電腦上為新來源提供現有來源的名稱, 所以在嘗試呼叫CreateEventSource之前, 請先使用此方法, 以確保電腦上不存在具有source指定名稱的來源。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. sourcemachineName參數不區分大小寫。The source and machineName parameters are not case sensitive.

SourceExistsstatic是方法, 因此可以在類別本身上呼叫。SourceExists is a static method, so it can be called on the class itself. 您不需要建立的EventLog實例來呼叫。 SourceExistsIt is not necessary to create an instance of EventLog to call SourceExists.

安全性

EventLogPermission
用於寫入電腦上的事件記錄檔資訊。for writing the event log information on the computer. 相關聯的列舉:WriteAssociated enumeration: Write

另請參閱

適用於