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;
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 。 这意味着,即使发生多个事件日志更改,也只会在六秒间隔内收到一 EntryWritten 个事件通知。 如果在调用 WriteEntry之间插入了足够长的睡眠间隔(大约 10 秒),则不太可能错过事件。 但是,如果写入事件更频繁地发生,则可能会在下一个间隔之前收到事件通知。 通常,错过的事件通知不会丢失,但延迟。

适用于

另请参阅