EventLog.EntryWritten EventLog.EntryWritten EventLog.EntryWritten EventLog.EntryWritten Event

定義

發生於項目寫入至本機電腦上的事件記錄檔時。Occurs when an entry is written to an event log on the local computer.

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

範例

下列範例會處理項目寫入事件。The following example handles an entry written event.

#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
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 ' Main


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

備註

若要取得事件通知,您必須設定EnableRaisingEventstrueTo get event notifications, you must set EnableRaisingEvents to true. 項目寫入本機電腦上時,您只可以接收事件通知。You can only receive event notifications when entries are written on the local computer. 您無法接收通知的遠端電腦上寫入的項目。You cannot receive notifications for entries written on remote computers.

當您建立 EntryWritten 委派 (Delegate) 時,就可以識別即將處理此事件的方法。When you create an EntryWritten delegate, you identify the method that will handle the event. 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。To associate the event with your event handler, add an instance of the delegate to the event. 每當事件發生時,直到您移除委派,會呼叫事件處理常式。The event handler is called whenever the event occurs, until you remove the delegate. 如需有關如何使用委派處理事件的詳細資訊,請參閱處理和引發事件For more information about handling events with delegates, see Handling and Raising Events.

系統會回應WriteEntry只有當最後一個寫入事件發生至少六秒之前。The system responds to WriteEntry only if the last write event occurred at least six seconds previously. 這表示您只會收到一個EntryWritten六秒的間隔內的事件通知,即使多個事件記錄的變更時發生。This implies you will only receive one EntryWritten event notification within a six-second interval, even if more than one event log change occurs. 如果您插入夠長的睡眠間隔 (大約 10 秒) 之間的呼叫WriteEntry,您不太可能遺漏事件。If you insert a sufficiently long sleep interval (around 10 seconds) between calls to WriteEntry, you are less likely to miss an event. 不過,如果寫入事件的發生頻率,您可能會收到事件通知,直到下一個間隔。However, if write events occur more frequently, you might not receive the event notification until the next interval. 一般而言,遺失的事件通知不會遺失,但延遲。Typically, missed event notifications are not lost, but delayed.

安全性

EventLogPermission
用於管理的電腦上的事件記錄檔資訊。for administering event log information on the computer. 相關聯的列舉型別: AdministerAssociated enumeration: Administer

適用於

另請參閱