DiagnosticListener.Subscribe 方法

定义

这些方法允许添加订阅者,其中一些方法提供可选的事件筛选方法。These methods allow adding a subscriber, and some of them provide optional event filtering methods.

重载

Subscribe(IObserver<KeyValuePair<String,Object>>)

添加订阅者。Adds a subscriber.

Subscribe(IObserver<KeyValuePair<String,Object>>, Func<String,Object,Object,Boolean>)

添加一个订阅者,根据事件名称和最多两个上下文对象筛选事件(可选)。Adds a subscriber, and optionally filters events based on their name and up to two context objects.

Subscribe(IObserver<KeyValuePair<String,Object>>, Predicate<String>)

添加一个订阅者,根据事件名称筛选事件(可选)。Adds a subscriber, and optionally filters events based on their name.

Subscribe(IObserver<KeyValuePair<String,Object>>, Func<String,Object,Object,Boolean>, Action<Activity,Object>, Action<Activity,Object>)

添加一个订阅者,根据事件名称和最多两个上下文对象筛选事件(可选),并指定提供程序在进程外部导入或导出活动时调用的方法。Adds a subscriber, optionally filters events based on their name and up to two context objects, and specifies methods to call when providers import or export activites from outside the process.

Subscribe(IObserver<KeyValuePair<String,Object>>)

添加订阅者。Adds a subscriber.

public:
 virtual IDisposable ^ Subscribe(IObserver<System::Collections::Generic::KeyValuePair<System::String ^, System::Object ^>> ^ observer);
public virtual IDisposable Subscribe (IObserver<System.Collections.Generic.KeyValuePair<string,object>>? observer);
public IDisposable Subscribe (IObserver<System.Collections.Generic.KeyValuePair<string,object>> observer);
public virtual IDisposable Subscribe (IObserver<System.Collections.Generic.KeyValuePair<string,object>> observer);
abstract member Subscribe : IObserver<System.Collections.Generic.KeyValuePair<string, obj>> -> IDisposable
override this.Subscribe : IObserver<System.Collections.Generic.KeyValuePair<string, obj>> -> IDisposable
Public Overridable Function Subscribe (observer As IObserver(Of KeyValuePair(Of String, Object))) As IDisposable
Public Function Subscribe (observer As IObserver(Of KeyValuePair(Of String, Object))) As IDisposable

参数

observer
IObserver<KeyValuePair<String,Object>>

订阅者。A subscriber.

返回

IDisposable

对允许侦听器在 DiagnosticSource 发送完通知前停止接收这些通知的接口的引用。A reference to an interface that allows the listener to stop receiving notifications before the DiagnosticSource has finished sending them.

实现

适用于

产品 Introduced

Subscribe(IObserver<KeyValuePair<String,Object>>, Func<String,Object,Object,Boolean>)

添加一个订阅者,根据事件名称和最多两个上下文对象筛选事件(可选)。Adds a subscriber, and optionally filters events based on their name and up to two context objects.

public:
 virtual IDisposable ^ Subscribe(IObserver<System::Collections::Generic::KeyValuePair<System::String ^, System::Object ^>> ^ observer, Func<System::String ^, System::Object ^, System::Object ^, bool> ^ isEnabled);
public virtual IDisposable Subscribe (IObserver<System.Collections.Generic.KeyValuePair<string,object>>? observer, Func<string,object,object,bool>? isEnabled);
public virtual IDisposable Subscribe (IObserver<System.Collections.Generic.KeyValuePair<string,object>> observer, Func<string,object,object,bool> isEnabled);
abstract member Subscribe : IObserver<System.Collections.Generic.KeyValuePair<string, obj>> * Func<string, obj, obj, bool> -> IDisposable
override this.Subscribe : IObserver<System.Collections.Generic.KeyValuePair<string, obj>> * Func<string, obj, obj, bool> -> IDisposable
Public Overridable Function Subscribe (observer As IObserver(Of KeyValuePair(Of String, Object)), isEnabled As Func(Of String, Object, Object, Boolean)) As IDisposable

参数

observer
IObserver<KeyValuePair<String,Object>>

订阅者。A subscriber.

isEnabled
Func<String,Object,Object,Boolean>

一个委托,它根据事件名称和最多两个上下文对象筛选事件(可为 null),如果不需要事件筛选器,则为 nullA delegate that filters events based on their name and up to two context objects (which can be null), or null to if an event filter is not desirable.

返回

IDisposable

对允许侦听器在 DiagnosticSource 发送完通知前停止接收这些通知的接口的引用。A reference to an interface that allows the listener to stop receiving notifications before the DiagnosticSource has finished sending them.

注解

如果不 isEnablednull ,则表示某些事件是不相关的,可跳过这些事件以提高效率。If isEnabled is not null, it indicates that some events are uninteresting can be skipped for efficiency.

特定检测站点可以选择调用一个或多个 IsEnabled 重载,其中它将事件的名称和多个其他 (检测站点特定) 对象作为参数传递。A particular instrumentation site has the option of calling one or more IsEnabled overloads in which it passes the name of the event and up to two other (instrumentation site specific) objects as arguments. 如果执行了这些调用中的任何一种 IsEnabled isEnabled ,则使用传递的值调用此谓词 (如果使用较短的重载, null 则会为缺少的上下文对象) 传递此谓词。If any of these IsEnabled calls are made then this isEnabled predicate is invoked with passed values (if shorter overloads are used, null is passed for missing context objects).

这为任何特定检测站点提供了将多达两个信息传递给订阅者的能力,以执行复杂的高效筛选。This gives any particular instrumentation site the ability to pass up to two pieces of information to the subscriber to do sophisticated, efficient filtering. 这需要检测站点和订阅服务器代码之间的更多耦合。This requires more coupling between the instrumentation site and the subscriber code.

预计特定检测站点可能会调用同一事件的不同重载 IsEnabled ,第一次调用时,将调用 IsEnabled(String) 具有两个上下文对象的筛选器 nullIt is expected that a particular instrumentation site may call different overloads of IsEnabled for the same event, first calling IsEnabled(String), which calls the filter with two null context objects. 如果 isEnabled 返回 true ,它将再次调用并返回上下文对象。If isEnabled returns true, it calls again with context objects. isEnabled此筛选器应在设计时考虑到这一点。The isEnabled filter should be designed with this in mind.

请注意, isEnabled 谓词是一个可选优化,它允许检测站点在 Write(String, Object) 没有订阅服务器关心的情况下避免设置负载和调用。Note that the isEnabled predicate is an optional optimization to allow the instrumentation site to avoid setting up the payload and calling Write(String, Object) when no subscriber cares about it. 具体而言,检测站点可以选择忽略 IsEnabled 谓词 (不) 调用它,只需调用 Write(String, Object)In particular, the instrumentation site has the option of ignoring the IsEnabled predicate (not calling it) and simply calling Write(String, Object). 因此,如果订阅服务器需要筛选,则需要进行筛选。Thus, if the subscriber requires the filtering, it needs to do it itself.

如果此参数为 null ,则不会 (所有返回) 的重载执行筛选 IsEnabled trueIf this parameter is null, no filtering is done (all overloads of IsEnabled return true).

适用于

产品 Introduced

Subscribe(IObserver<KeyValuePair<String,Object>>, Predicate<String>)

添加一个订阅者,根据事件名称筛选事件(可选)。Adds a subscriber, and optionally filters events based on their name.

public:
 virtual IDisposable ^ Subscribe(IObserver<System::Collections::Generic::KeyValuePair<System::String ^, System::Object ^>> ^ observer, Predicate<System::String ^> ^ isEnabled);
public virtual IDisposable Subscribe (IObserver<System.Collections.Generic.KeyValuePair<string,object>>? observer, Predicate<string>? isEnabled);
public virtual IDisposable Subscribe (IObserver<System.Collections.Generic.KeyValuePair<string,object>> observer, Predicate<string> isEnabled);
abstract member Subscribe : IObserver<System.Collections.Generic.KeyValuePair<string, obj>> * Predicate<string> -> IDisposable
override this.Subscribe : IObserver<System.Collections.Generic.KeyValuePair<string, obj>> * Predicate<string> -> IDisposable
Public Overridable Function Subscribe (observer As IObserver(Of KeyValuePair(Of String, Object)), isEnabled As Predicate(Of String)) As IDisposable

参数

observer
IObserver<KeyValuePair<String,Object>>

订阅者。A subscriber.

isEnabled
Predicate<String>

一个委托,它根据其名称 (String) 筛选事件。A delegate that filters events based on their name (String). 如果已启用事件,则委托应返回 trueThe delegate should return true if the event is enabled.

返回

IDisposable

对允许侦听器在 DiagnosticSource 发送完通知前停止接收这些通知的接口的引用。A reference to an interface that allows the listener to stop receiving notifications before the DiagnosticSource has finished sending them.

注解

如果不 isEnablednull ,则有些事件是不相关的,可跳过效率。If isEnabled is not null, some events are uninteresting and can be skipped for efficiency. isEnabled谓词是一种可选优化,用于允许检测站点在 Write(String, Object) 没有订阅服务器关心的情况时避免设置负载和调用。The isEnabled predicate is an optional optimization to allow the instrumentation site to avoid setting up the payload and calling Write(String, Object) when no subscriber cares about it. 具体而言,检测站点可以选择忽略 IsEnabled() 谓词 (不) 调用它,只需调用 Write(String, Object)In particular the instrumentation site has the option of ignoring the IsEnabled() predicate (not calling it) and simply calling Write(String, Object). 因此,如果订阅服务器需要筛选,则需要进行筛选。Thus if the subscriber requires the filtering, it needs to do it itself.

如果 isEnablednull ,则不会 (所有返回) 的重载执行筛选 IsEnabled trueIf isEnabled is null, no filtering is done (all overloads of IsEnabled return true).

适用于

产品 Introduced

Subscribe(IObserver<KeyValuePair<String,Object>>, Func<String,Object,Object,Boolean>, Action<Activity,Object>, Action<Activity,Object>)

添加一个订阅者,根据事件名称和最多两个上下文对象筛选事件(可选),并指定提供程序在进程外部导入或导出活动时调用的方法。Adds a subscriber, optionally filters events based on their name and up to two context objects, and specifies methods to call when providers import or export activites from outside the process.

public virtual IDisposable Subscribe (IObserver<System.Collections.Generic.KeyValuePair<string,object>>? observer, Func<string,object,object,bool>? isEnabled, Action<System.Diagnostics.Activity,object>? onActivityImport = default, Action<System.Diagnostics.Activity,object>? onActivityExport = default);
public virtual IDisposable Subscribe (IObserver<System.Collections.Generic.KeyValuePair<string,object>> observer, Func<string,object,object,bool> isEnabled, Action<System.Diagnostics.Activity,object> onActivityImport = default, Action<System.Diagnostics.Activity,object> onActivityExport = default);
abstract member Subscribe : IObserver<System.Collections.Generic.KeyValuePair<string, obj>> * Func<string, obj, obj, bool> * Action<System.Diagnostics.Activity, obj> * Action<System.Diagnostics.Activity, obj> -> IDisposable
override this.Subscribe : IObserver<System.Collections.Generic.KeyValuePair<string, obj>> * Func<string, obj, obj, bool> * Action<System.Diagnostics.Activity, obj> * Action<System.Diagnostics.Activity, obj> -> IDisposable
Public Overridable Function Subscribe (observer As IObserver(Of KeyValuePair(Of String, Object)), isEnabled As Func(Of String, Object, Object, Boolean), Optional onActivityImport As Action(Of Activity, Object) = Nothing, Optional onActivityExport As Action(Of Activity, Object) = Nothing) As IDisposable

参数

observer
IObserver<KeyValuePair<String,Object>>

订阅者。A subscriber.

isEnabled
Func<String,Object,Object,Boolean>

一个委托,它根据事件名称和最多两个上下文对象筛选事件(可为 null),如果不需要事件筛选器,则为 nullA delegate that filters events based on their name and up to two context objects (which can be null), or null if an event filter is not desirable.

onActivityImport
Action<Activity,Object>

一个操作委托,用于接收由外部事件影响的活动以及表示传入请求的对象。An action delegate that receives the activity affected by an external event and an object that represents the incoming request.

onActivityExport
Action<Activity,Object>

一个操作委托,用于接收由外部事件影响的活动以及表示传出请求的对象。An action delegate that receives the activity affected by an external event and an object that represents the outgoing request.

返回

IDisposable

对允许侦听器在 DiagnosticSource 发送完通知前停止接收这些通知的接口的引用。A reference to an interface that allows the listener to stop receiving notifications before the DiagnosticSource has finished sending them.

注解

如果 isEnabled 为非 null,则可以跳过某些事件,从而提高效率。If isEnabled is non-null, some events are uninteresting can be skipped for efficiency.

你还可以提供 "onActivityImport" 和 "onActivityExport" 方法,这些方法在提供程序从进程外部导入或导出活动时调用 (例如,从) HTTP 请求。You can also supply 'onActivityImport' and 'onActivityExport' methods that are called when providers importing or export activities from outside the process (for example, from HTTP requests). 这些方法在导入或导出活动后调用,可用于修改活动或添加策略的传出请求。These methods are called after importing or exporting the activity and can be used to modify the activity or the outgoing request to add policy.

适用于

产品 Introduced