EventListener 类


提供用于启用和禁用事件源中事件的方法。Provides methods for enabling and disabling events from event sources.

public ref class EventListener abstract : IDisposable
public ref class EventListener : IDisposable
public abstract class EventListener : IDisposable
public class EventListener : IDisposable
type EventListener = class
    interface IDisposable
Public MustInherit Class EventListener
Implements IDisposable
Public Class EventListener
Implements IDisposable


事件侦听器表示 EventSource 当前应用程序域中由事件源 (对象) 实现生成的所有事件的目标。An event listener represents the target for all events generated by event source (EventSource object) implementations in the current application domain. 创建新的事件侦听器后,它将以逻辑方式附加到该应用程序域中的所有事件源。When a new event listener is created, it is logically attached to all event sources in that application domain.


此类型实现 IDisposable 接口。This type implements the IDisposable interface. 使用完类型后,必须直接或间接地释放它。When you have finished using the type, you must dispose of it either directly or indirectly. 若要直接释放类型,请在 try/catch 块中调用其 Dispose 方法。To dispose of the type directly, call its Dispose method in a try/catch block. 若要间接释放类型,请使用 using(在 C# 中)或 Using(在 Visual Basic 中)等语言构造。To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

事件侦听器可以基于每个事件源启用或禁用事件级别 (EventLevel) 和事件关键字 (EventKeywords) 进一步限制要发送到侦听器的事件集。An event listener can enable or disable on a per-current event source basis by using event levels (EventLevel) and event keywords (EventKeywords) to further restrict the set of events to be sent to the listener. 侦听器还可以使用方法向特定的当前事件源发送任意命令 SendCommandThe listener can also send arbitrary commands to a specific current event source by using the SendCommand method. 命令的含义特定于事件源的实现。The meanings of the commands are specific to the implementation of the event source.

可以在单个应用程序域中定义多个事件侦听器。Multiple event listeners can be defined in a single application domain. 每个侦听器在逻辑上独立于其他侦听器,以便一个侦听器启用或禁用的事件只影响该侦听器。Each listener is logically independent of the other listeners so that events that are enabled or disabled by one listener affect only that listener. 使用方法发送的命令有可能 SendCommand 执行语义操作 (如垃圾回收或刷新影响其他侦听器的数据) ,但这是例外,而不是规则。It is possible for commands sent with the SendCommand method to do a semantic operation (such as a garbage collection or flushing data) that affects the other listeners, but this is the exception rather than the rule.

每个事件源保留向其发送事件的事件侦听器的列表。Each event source keeps a list of event listeners that it sends events to. 一组筛选条件与每个事件源侦听器对关联。A set of filtering criteria is associated with each event source-listener pair. 这些条件确定侦听器将为当前事件源接收的事件。These criteria determine the events that listener will receive for the current event source. 侦听器会接收其方法的事件 OnEventWrittenListeners receive the events on their OnEventWritten method. 因此,的子类 EventListener 必须重写此方法,以便对数据执行一些有用的操作。Therefore, subclasses of EventListener must override this method to do something useful with the data.

创建新的事件源时,将 OnEventSourceCreated 调用方法。When a new event source is created, the OnEventSourceCreated method is called. 每个当前事件侦听器每个 OnEventSourceCreated 可发送日志消息的当前事件源只获得一个调用。Every current event listener gets exactly one OnEventSourceCreated call for every current event source that can send it log messages. 创建事件侦听器时, OnEventSourceCreated 会进行一系列调用,通知创建侦听器之前存在的所有事件源的新侦听器。When an event listener is created, a series of OnEventSourceCreated calls are made to notify the new listener of all the event sources that existed before the listener was created.



创建 EventListener 类的新实例。Creates a new instance of the EventListener class.



禁用指定事件源的所有事件。Disables all events for the specified event source.


释放 EventListener 类的当前实例使用的资源。Releases the resources used by the current instance of the EventListener class.

EnableEvents(EventSource, EventLevel)

启用具有指定详细级别或更低详细级别的指定事件源的事件。Enables events for the specified event source that has the specified verbosity level or lower.

EnableEvents(EventSource, EventLevel, EventKeywords)

启动具有指定详细级别或更低详细级别且与关键字标志匹配的指定事件源的事件。Enables events for the specified event source that has the specified verbosity level or lower, and matching keyword flags.

EnableEvents(EventSource, EventLevel, EventKeywords, IDictionary<String,String>)

启动具有指定详细级别或更低详细级别且与关键字标志和自变量匹配的指定事件源的事件。Enables events for the specified event source that has the specified verbosity level or lower, matching event keyword flag, and matching arguments.


确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

获取表示指定事件源的较小非负数。Gets a small non-negative number that represents the specified event source.


作为默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

当创建该事件侦听器且将新事件源附加到侦听器时,对所有现有事件源执行了调用。Called for all existing event sources when the event listener is created and when a new event source is attached to the listener.


每次事件源写入事件时都执行调用,其中事件侦听器为事件源启用了事件。Called whenever an event has been written by an event source for which the event listener has enabled events.


返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)



当事件源(EventSource 对象)附加到调度程序时发生。Occurs when an event source (EventSource object) is attached to the dispatcher.


当事件已由事件源(EventSource 对象)写入时发生,其中事件侦听器已为该事件源启用了事件。Occurs when an event has been written by an event source (EventSource object) for which the event listener has enabled events.