EventLog.SourceExists メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
コンピューターのレジストリで特定のイベント ソースを検索します。
オーバーロード
| 名前 | 説明 |
|---|---|
| SourceExists(String) |
イベント ソースがローカル コンピューターに登録されているかどうかを判断します。 |
| SourceExists(String, String) |
指定したコンピューターにイベント ソースが登録されているかどうかを判断します。 |
SourceExists(String)
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
イベント ソースがローカル コンピューターに登録されているかどうかを判断します。
public:
static bool SourceExists(System::String ^ source);
public static bool SourceExists(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;
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 がスローされます。
Note
Windows Vista 以降または 2003 Windows Serverでイベント ソースを検索するには、管理者特権が必要です。
この要件の理由は、セキュリティを含むすべてのイベント ログを検索して、イベント ソースが一意かどうかを判断する必要があるということです。 Windows Vista 以降、ユーザーはセキュリティログへのアクセス許可を持っていません。このため、SecurityException がスローされます。
Windows Vista 以降では、ユーザー アカウント制御 (UAC) によってユーザーの権限が決定されます。 組み込みの Administrators グループのメンバーである場合は、標準ユーザー アクセス トークンと管理者アクセス トークンという 2 つのランタイム アクセス トークンが割り当てられます。 既定では、標準のユーザー ロールになります。 パフォーマンス カウンターにアクセスするコードを実行するには、まず特権を標準ユーザーから管理者に昇格させる必要があります。 これを行うには、アプリケーション アイコンを右クリックし、管理者として実行することを示すことで、アプリケーションを起動します。
Note
LocalSystem アカウントで実行されているサービスには、このメソッドを実行するために必要な特権がありません。 解決策は、イベント ソースが ServiceInstallerに存在するかどうかを確認し、存在しない場合は、インストーラーでソースを作成することです。
同じコンピューター上の既存のソースの名前を新しいソースに指定できないため、 CreateEventSource を呼び出して、 source で指定された名前のソースがローカル コンピューターにまだ存在していないことを確認する前に、このメソッドを使用してください。
source パラメーターでは、大文字と小文字は区別されません。
こちらもご覧ください
適用対象
SourceExists(String, String)
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
- ソース:
- EventLog.cs
指定したコンピューターにイベント ソースが登録されているかどうかを判断します。
public:
static bool SourceExists(System::String ^ source, System::String ^ machineName);
public static bool SourceExists(string? source, 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にソース MyServerを作成し、イベント ログ MyNewLogにエントリを書き込みます。
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 がスローされます。
Note
Windows Vista 以降または 2003 Windows Serverでイベント ソースを検索するには、管理者特権が必要です。
この要件の理由は、セキュリティを含むすべてのイベント ログを検索して、イベント ソースが一意かどうかを判断する必要があるということです。 Windows Vista 以降、ユーザーはセキュリティログへのアクセス許可を持っていません。このため、SecurityException がスローされます。
Windows Vista 以降では、ユーザー アカウント制御 (UAC) によってユーザーの権限が決定されます。 組み込みの Administrators グループのメンバーである場合は、標準ユーザー アクセス トークンと管理者アクセス トークンという 2 つのランタイム アクセス トークンが割り当てられます。 既定では、標準のユーザー ロールになります。 パフォーマンス カウンターにアクセスするコードを実行するには、まず特権を標準ユーザーから管理者に昇格させる必要があります。 これを行うには、アプリケーション アイコンを右クリックし、管理者として実行することを示すことで、アプリケーションを起動します。
Note
LocalSystem アカウントで実行されているサービスには、このメソッドを実行するために必要な特権がありません。 解決策は、イベント ソースが ServiceInstallerに存在するかどうかを確認し、存在しない場合は、インストーラーでソースを作成することです。
同じコンピューター上の既存のソースの名前を新しいソースに指定できないため、 CreateEventSource を呼び出して、 source で指定された名前のソースがまだコンピューターに存在していないことを確認する前に、このメソッドを使用してください。
sourceパラメーターと machineName パラメーターでは、大文字と小文字は区別されません。
SourceExists は static メソッドであるため、クラス自体で呼び出すことができます。
EventLogを呼び出すためにSourceExistsのインスタンスを作成する必要はありません。