EventLog.SourceExists 方法

定義

搜尋指定事件來源的電腦的登錄。

多載

SourceExists(String)

判斷事件來源是否登錄於本機電腦上。

SourceExists(String, String)

判斷事件來源是否登錄於指定的電腦上。

SourceExists(String)

來源:
EventLog.cs
來源:
EventLog.cs
來源:
EventLog.cs

判斷事件來源是否登錄於本機電腦上。

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

事件來源的名稱。

傳回

如果事件來源登錄於本機電腦上,則為 true,否則為 false

例外狀況

找不到 source,但無法搜尋部分或全部的事件記錄檔。

範例

下列範例會在來源不存在時建立來源 MySource ,並將專案寫入事件記錄檔 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

備註

使用這個方法來判斷事件來源是否存在於本機計算機上。 如果您要判斷本機電腦上是否有記錄,請使用 Exists

因為這個方法會存取登錄,所以您必須在本機計算機上具有適當的登錄許可權;否則, SecurityException 將會擲回 。

注意

若要在 Windows Vista 和更新版本或 Windows Server 2003 中搜尋事件來源,您必須具有系統管理許可權。

這項需求的原因是必須搜尋所有事件記錄檔,包括安全性,才能判斷事件來源是否是唯一的。 從 Windows Vista 開始,用戶沒有存取安全性記錄的許可權;因此, SecurityException 擲回 。

從 Windows Vista 開始,用戶帳戶控制 (UAC) 決定使用者的許可權。 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。 根據預設,您會屬於標準使用者角色。 若要執行存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 您可以在啟動應用程式時,以滑鼠右鍵按一下應用程式圖示,並指出您想要以系統管理員身分執行,藉此提高為系統管理員權限。

注意

在帳戶下 LocalSystem 執行的服務沒有執行此方法所需的許可權。 解決方案是在 中檢查事件來源是否存在 ServiceInstaller,如果事件來源不存在,請在安裝程式中建立來源。

因為您無法為新來源提供相同電腦上的現有來源名稱,所以在嘗試呼叫 CreateEventSource 之前,請先使用此方法,以確保本機計算機上沒有指定 source 名稱的來源。 參數 source 不區分大小寫。

另請參閱

適用於

SourceExists(String, String)

來源:
EventLog.cs
來源:
EventLog.cs
來源:
EventLog.cs

判斷事件來源是否登錄於指定的電腦上。

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

參數

source
String

事件來源的名稱。

machineName
String

要在其上進行尋找的電腦的名稱,或者表示本機電腦的 "."。

傳回

如果事件來源註冊於指定的電腦上,則為 true;否則為 false

例外狀況

machineName 為無效的電腦名稱。

找不到 source,但無法搜尋部分或全部的事件記錄檔。

範例

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

備註

使用這個方法來判斷事件來源是否存在於 參數所 machineName 指定的計算機上。 如果您要判斷指定的電腦上是否有記錄,請使用 Exists

因為這個方法會存取登錄,所以您必須在指定的伺服器上擁有適當的登錄許可權;否則, SecurityException 將會擲回 。

注意

若要在 Windows Vista 和更新版本或 Windows Server 2003 中搜尋事件來源,您必須具有系統管理許可權。

這項需求的原因是必須搜尋所有事件記錄檔,包括安全性,才能判斷事件來源是否是唯一的。 從 Windows Vista 開始,用戶沒有存取安全性記錄的許可權;因此, SecurityException 擲回 。

從 Windows Vista 開始,用戶帳戶控制 (UAC) 決定使用者的許可權。 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。 根據預設,您會屬於標準使用者角色。 若要執行存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 您可以在啟動應用程式時,以滑鼠右鍵按一下應用程式圖示,並指出您想要以系統管理員身分執行,藉此提高為系統管理員權限。

注意

在帳戶下 LocalSystem 執行的服務沒有執行此方法所需的許可權。 解決方案是在 中檢查事件來源是否存在 ServiceInstaller,如果事件來源不存在,請在安裝程式中建立來源。

因為您無法為新來源提供同一部計算機上的現有來源名稱,所以請先使用這個方法,再嘗試呼叫 CreateEventSource 以確保計算機上沒有指定 source 名稱的來源。 sourcemachineName 參數不區分大小寫。

SourceExistsstatic是方法,因此可以在類別本身上呼叫。 不需要建立的 EventLog 實體來呼叫 SourceExists

另請參閱

適用於