次の方法で共有


EventLog.SourceExists メソッド (String, String)

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

Overloads Public Shared Function SourceExists( _
   ByVal source As String, _   ByVal machineName As String _) As Boolean
[C#]
public static bool SourceExists(stringsource,stringmachineName);
[C++]
public: static bool SourceExists(String* source,String* machineName);
[JScript]
public static function SourceExists(
   source : String,machineName : String) : Boolean;

パラメータ

  • source
    イベント ソースの名前。
  • machineName
    検索対象となるコンピュータの名前。ローカル コンピュータの場合は空の文字列 ("")。

戻り値

指定したコンピュータにイベント ソースが存在する場合は true 。それ以外の場合は false

例外

例外の種類 条件
ArgumentException machineName パラメータに無効なコンピュータ名が指定されています。

解説

このメソッドは、 machineName で指定したコンピュータにイベント ソースが存在するかどうかを確認するときに使用します。指定したコンピュータにログが存在するかどうかを確認する場合は、 Exists を使用してください。

このメソッドはレジストリにアクセスするため、指定したサーバー上で適切なレジストリ アクセス許可を持っている必要があります。このアクセス許可がない場合、クエリは false を返します。

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

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

使用例

[Visual Basic, C#, C++] コンピュータ "MyServer" 上のイベント ログ "MyNewLog" にエントリを書き込み、ソースがまだ存在しない場合はソース "MySource" を作成する例を次に示します。

[Visual Basic, C#, C++] メモ   コード内でイベント ソースを作成する必要はありません。 Source プロパティで設定したソースが存在しない場合、 WriteEntry メソッドは、イベント ログに書き込む前に、そのソースを作成します。 EventLog インスタンスの 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

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

[C++] 
#using <mscorlib.dll>
#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(S"MySource", S"MyServer")){
        EventLog::CreateEventSource(S"MySource", S"MyNewLog", S"MyServer");
        Console::WriteLine(S"CreatingEventSource");
    }

    // Create an EventLog instance and assign its source.
    EventLog* myLog = new EventLog();
    myLog->Source = S"MySource";

    // Write an informational entry to the event log.    
    myLog->WriteEntry(S"Writing to event log.");

    Console::WriteLine(S"Message written to event log.");                                                                        
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

EventLog クラス | EventLog メンバ | System.Diagnostics 名前空間 | EventLog.SourceExists オーバーロードの一覧 | CreateEventSource | DeleteEventSource | Exists | Source | MachineName