ローカル コンピュータ上のすべてのイベント ログを検索し、リストを格納する EventLog オブジェクトの配列を作成します。
Overloads Public Shared Function GetEventLogs() As EventLog()
[C#]
public static EventLog[] GetEventLogs();
[C++]
public: static EventLog* GetEventLogs() [];
[JScript]
public static function GetEventLogs() : EventLog[];
戻り値
ローカル コンピュータ上のログを表す EventLog 型の配列。
例外
例外の種類 | 条件 |
---|---|
SystemException | レジストリの読み取りアクセス許可がありません。
または コンピュータ上にイベント ログ サービスがありません。 |
解説
EventLog オブジェクトの配列は、 GetEventLogs を呼び出した時点でローカル コンピュータに存在するすべてのイベント ログのスナップショットです。これは動的なコレクションではないため、ログの削除や作成はリアルタイムには反映されません。配列内のログの読み取りや書き込みを行う前に、配列内にログが存在するかどうかを確認してください。通常、配列には少なくともアプリケーション ログ、システム ログ、セキュリティ ログの 3 つのログが格納されています。ローカル コンピュータにカスタム ログを作成した場合は、それらのカスタム ログも配列内に表示されます。
イベント ログのリストを取得するには、適切なレジストリ アクセス許可を持っている必要があります。これらのアクセス許可は、 Exists や SourceExists を呼び出すときに必要なアクセス許可と同じです。
使用例
[Visual Basic, C#, C++] ローカル コンピュータ上のログのリストを取得する例を次に示します。その後で、各ログの名前を出力します。
Option Explicit
Option Strict
Imports System
Imports System.Diagnostics
Imports System.Threading
Class MySample
Public Shared Sub Main()
Dim localEventLogs() As EventLog
localEventLogs = EventLog.GetEventLogs()
Console.WriteLine(("Number of logs on local computer: " & localEventLogs.Length))
Dim log As EventLog
For Each log In localEventLogs
Console.WriteLine(("Log: " & log.Log))
Next log
End Sub ' Main
End Class ' MySample
[C#]
using System;
using System.Diagnostics;
using System.Threading;
class MySample{
public static void Main(){
EventLog[] localEventLogs;
localEventLogs = EventLog.GetEventLogs();
Console.WriteLine("Number of logs on local computer: " + localEventLogs.Length);
foreach(EventLog log in localEventLogs){
Console.WriteLine("Log: " + log.Log);
}
}
}
[C++]
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main(){
EventLog* localEventLogs[];
localEventLogs = EventLog::GetEventLogs();
Console::WriteLine(S"Number of logs on local computer: {0}", __box(localEventLogs->Length));
System::Collections::IEnumerator* myEnum = localEventLogs->GetEnumerator();
while (myEnum->MoveNext())
{
EventLog* log = __try_cast<EventLog*>(myEnum->Current);
Console::WriteLine(S"Log: {0}", log->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 セキュリティ:
- EventLogPermission (コンピュータ上のイベント ログ情報にアクセスするのに必要なアクセス許可)
- RegistryPermission (コンピュータ上のイベント ログ レジストリ情報にアクセスするのに必要なアクセス許可)
- SecurityPermission (完全信頼を指定して EventLog のメンバを呼び出すためのアクセス許可) PermissionState.Unrestricted (関連する列挙体)
参照
EventLog クラス | EventLog メンバ | System.Diagnostics 名前空間 | EventLog.GetEventLogs オーバーロードの一覧 | Exists | Log