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 委托时,需要标识将要处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 每当事件发生时,将调用事件处理程序,直到删除委托。 有关使用委托处理事件的详细信息,请参阅 处理和引发事件

仅当最后一次写入事件至少发生在六秒前,系统才会响应 WriteEntry 。 这意味着,即使发生多个事件日志更改,也不会在 6 秒间隔内收到 EntryWritten 一个事件通知。 如果在调用 WriteEntry之间插入足够长的睡眠间隔 (大约 10 秒) ,则不太可能错过事件。 但是,如果写入事件更频繁发生,则可能在下一个间隔之前不会收到事件通知。 通常,错过的事件通知不会丢失,而是延迟。

适用于

另请参阅