共用方式為


EventLog.EntryWritten 事件

定義

發生於項目寫入至本機電腦上的事件記錄檔時。

public:
 event System::Diagnostics::EntryWrittenEventHandler ^ EntryWritten;
public event System.Diagnostics.EntryWrittenEventHandler EntryWritten;
member this.EntryWritten : System.Diagnostics.EntryWrittenEventHandler 
Public Custom Event EntryWritten As EntryWrittenEventHandler 

事件類型

範例

下列範例會處理寫入的專案。

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
ref class MySample
{
private:

   // This member is used to wait for events.
   static AutoResetEvent^ signal;

public:
   static void main()
   {
      signal = gcnew AutoResetEvent( false );
      EventLog^ myNewLog = gcnew EventLog;
      myNewLog->Source = "testEventLogEvent";
      myNewLog->EntryWritten += gcnew EntryWrittenEventHandler( MyOnEntryWritten );
      myNewLog->EnableRaisingEvents = true;
      myNewLog->WriteEntry("Test message", EventLogEntryType::Information);
      signal->WaitOne();
   }

   static void MyOnEntryWritten( Object^ /*source*/, EntryWrittenEventArgs^ /*e*/ )
   {
      Console::WriteLine("In event handler");
      signal->Set();
   }

};

int main()
{
   MySample::main();
}
using System;
using System.Diagnostics;
using System.Threading;

class MySample{

    // This member is used to wait for events.
    static AutoResetEvent signal;

    public static void Main(){

        signal = new AutoResetEvent(false);
        EventLog myNewLog = new EventLog("Application", ".", "testEventLogEvent");

        myNewLog.EntryWritten += new EntryWrittenEventHandler(MyOnEntryWritten);
        myNewLog.EnableRaisingEvents = true;
        myNewLog.WriteEntry("Test message", EventLogEntryType.Information);
        signal.WaitOne();
    }

    public static void MyOnEntryWritten(object source, EntryWrittenEventArgs e){
        Console.WriteLine("In event handler");
        signal.Set();
    }
}
Option Explicit On 
Option Strict On

Imports System.Diagnostics
Imports System.Threading


Class MySample

    ' This member is used to wait for events.
    Private Shared signal As AutoResetEvent


    Public Shared Sub Main()

        signal = New AutoResetEvent(False)
        Dim myNewLog As New EventLog("Application", ".", "testEventLogEvent")

        AddHandler myNewLog.EntryWritten, AddressOf MyOnEntryWritten
        myNewLog.EnableRaisingEvents = True
        myNewLog.WriteEntry("Test message", EventLogEntryType.Information)

        signal.WaitOne()
    End Sub


    Public Shared Sub MyOnEntryWritten(ByVal [source] As Object, ByVal e As EntryWrittenEventArgs)
        Console.WriteLine("In event handler")
        signal.Set()
    End Sub
End Class

備註

若要取得事件通知,您必須設定 EnableRaisingEventstrue。 您只能在本機電腦上寫入專案時收到事件通知。 您無法收到遠端電腦上寫入專案的通知。

當您建立 EntryWritten 委派 (Delegate) 時,就可以識別即將處理此事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 每當事件發生時,都會呼叫事件處理程式,直到您移除委派為止。 如需使用委派處理事件的詳細資訊,請參閱 處理和引發事件

只有在上次寫入事件之前至少發生六秒時,系統才會回應 WriteEntry 。 這表示您只會在六秒間隔內收到一個 EntryWritten 事件通知,即使發生多個事件記錄檔變更也一樣。 如果您在呼叫 WriteEntry之間插入夠長的睡眠間隔 (大約 10 秒) ,則不太可能錯過事件。 不過,如果寫入事件更頻繁地發生,在下次間隔之前,您可能不會收到事件通知。 一般而言,遺漏的事件通知不會遺失,但延遲。

適用於

另請參閱