次の方法で共有


EventLog.SourceExists メソッド (String)

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

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

パラメータ

  • source
    イベント ソースの名前。

戻り値

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

解説

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

このメソッドはレジストリにアクセスするため、ローカル コンピュータ上で適切なレジストリ アクセス許可を持っている必要があります。アクセス許可がない場合、クエリは false を返します。

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

使用例

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

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

[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")){
            EventLog.CreateEventSource("MySource", "MyNewLog");
            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")){
        EventLog::CreateEventSource(S"MySource", S"MyNewLog");
        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