EventLog.SourceExists メソッド

定義

コンピューターのレジストリ内で、指定したイベント ソースを検索します。

オーバーロード

SourceExists(String)

ローカル コンピューターにイベント ソースが登録されているかどうかを確認します。

SourceExists(String, String)

指定したコンピューターにイベント ソースが登録されているかどうかを確認します。

SourceExists(String)

ローカル コンピューターにイベント ソースが登録されているかどうかを確認します。

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 グループのメンバーである場合、そのユーザーには標準ユーザー アクセス トークンおよび管理者アクセス トークンの 2 つのランタイム アクセス トークンが割り当てられています。 既定では、ユーザーは標準ユーザー ロールに所属します。 パフォーマンス カウンターにアクセスするコードを実行するには、まず特権を標準ユーザーから管理者に昇格させる必要があります。 この操作は、アプリケーションの起動時にアプリケーション アイコンを右クリックし、管理者として実行することを指定して行うことができます。

注意

アカウントで LocalSystem 実行されているサービスには、このメソッドを実行するために必要な特権がありません。 解決策は、イベント ソースが に ServiceInstaller存在するかどうかを確認し、存在しない場合はインストーラーでソースを作成することです。

同じコンピューター上の既存のソースの名前を新しいソースに指定できないため、 を呼び出 CreateEventSource して、 で source 指定された名前のソースがローカル コンピューターにまだ存在しないことを確認する前に、このメソッドを使用してください。 パラメーターでは source 大文字と小文字は区別されません。

こちらもご覧ください

適用対象

SourceExists(String, String)

指定したコンピューターにイベント ソースが登録されているかどうかを確認します。

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 が見つかりませんでしたが、イベント ログの一部またはすべてが検索できませんでした。

次の例では、 コンピューターにソースMySourceを作成し、イベント ログ にエントリを書き込みますMyNewLogMyServer

#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 グループのメンバーである場合、そのユーザーには標準ユーザー アクセス トークンおよび管理者アクセス トークンの 2 つのランタイム アクセス トークンが割り当てられています。 既定では、ユーザーは標準ユーザー ロールに所属します。 パフォーマンス カウンターにアクセスするコードを実行するには、まず特権を標準ユーザーから管理者に昇格させる必要があります。 この操作は、アプリケーションの起動時にアプリケーション アイコンを右クリックし、管理者として実行することを指定して行うことができます。

注意

アカウントで LocalSystem 実行されているサービスには、このメソッドを実行するために必要な特権がありません。 解決策は、イベント ソースが に ServiceInstaller存在するかどうかを確認し、存在しない場合はインストーラーでソースを作成することです。

同じコンピューター上の既存のソースの名前を新しいソースに指定できないため、 を呼び出 CreateEventSource す前にこのメソッドを使用して、 で source 指定された名前のソースがコンピューターにまだ存在しないことを確認してください。 sourceパラメーターと パラメーターでは、大文字とmachineName小文字は区別されません。

SourceExistsstaticは メソッドであるため、クラス自体で呼び出すことができます。 を呼び出SourceExistsすために のEventLogインスタンスを作成する必要はありません。

こちらもご覧ください

適用対象