DiagnosticListener
public class DiagnosticListener : DiagnosticSource, IObservable<KeyValuePair<string, object>>, IDisposable
A DiagnosticListener is something that forwards on events written with DiagnosticSource.
It is an IObservable (has Subscribe method), and it also has a Subscribe overloads that
lets you specify a 'IsEnabled' predicate that users of DiagnosticSource will use for
'quick checks'.
The item in the stream is a KeyValuePair[string, object] where the string is the name
of the diagnostic item and the object is the payload (typically an anonymous type).
There may be many DiagnosticListeners in the system, but we encourage the use of
The DiagnosticSource.DefaultSource which goes to the DiagnosticListener.DefaultListener.
If you need to see 'everything' you can subscribe to the 'AllListeners' event that
will fire for every live DiagnosticListener in the appdomain (past or present).
Please See the DiagnosticSource Users Guide
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.DiagnosticSource/src/DiagnosticSourceUsersGuide.md
for instructions on its use.
When you subscribe to this you get callbacks for all NotificationListeners in the appdomain
as well as those that occurred in the past, and all future Listeners created in the future.
When a DiagnosticListener is created it is given a name. Return this.
Make a new DiagnosticListener, it is a NotificationSource, which means the returned result can be used to
log notifications, but it also has a Subscribe method so notifications can be forwarded
arbitrarily. Thus its job is to forward things from the producer to all the listeners
(multi-casting). Generally you should not be making your own DiagnosticListener but use the
DiagnosticListener.Default, so that notifications are as 'public' as possible.
Clean up the NotificationListeners. Notification listeners do NOT DIE ON THEIR OWN
because they are in a global list (for discoverability). You must dispose them explicitly.
Note that we do not do the Dispose(bool) pattern because we frankly don't want to support
subclasses that have non-managed state.
Determines whether there are any registered subscribers
public virtual IDisposable Subscribe(IObserver<KeyValuePair<string, object>> observer, Func<string, object, object, bool> isEnabled, Action<Activity, object> onActivityImport = null, Action<Activity, object> onActivityExport = null)
Add a subscriber (Observer). If the isEnabled parameter is non-null indicates that some events are
uninteresting can be skipped for efficiency. You can also supply an 'onActivityImport' and 'onActivityExport'
methods that should be called when providers are 'importing' or 'exporting' activities from outside the
process (e.g. from Http Requests). These are called right after importing (exporting) the activity and
can be used to modify the activity (or outgoing request) to add policy.
public virtual IDisposable Subscribe(IObserver<KeyValuePair<string, object>> observer, Predicate<string> isEnabled)
Add a subscriber (Observer). If the isEnabled parameter is non-null it indicates that some events are
uninteresting and can be skipped for efficiency.
public virtual IDisposable Subscribe(IObserver<KeyValuePair<string, object>> observer, Func<string, object, object, bool> isEnabled)
Add a subscriber (Observer). If the isEnabled parameter is non-null indicates that some events are
uninteresting can be skipped for efficiency.
Same as other Subscribe overload where the predicate is assumed to always return true.