Skip to content

Commit

Permalink
Update diagnosticsource-diagnosticlistener.md (#39653)
Browse files Browse the repository at this point in the history
Porting the change in dotnet/runtime#98801

Calling out that DiagnosticListener may receive events that don't satisfy the provided filter.
  • Loading branch information
noahfalk committed Feb 24, 2024
1 parent 8c8b246 commit cfea110
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/core/diagnostics/diagnosticsource-diagnosticlistener.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,22 @@ The `listener.Subscribe()` call in the previous example can be replaced with the
This efficiently subscribes to only the 'RequestStart' events. All other events will cause the `DiagnosticSource.IsEnabled()`
method to return `false` and thus be efficiently filtered out.

> [!NOTE]
> Filtering is only designed as a performance optimization. It is possible for a listener to receive events even when they
> do not satisfy the filter. This could occur because some other listener has subscribed to the event or because the source
> of the event didn't check IsEnabled() prior to sending it. If you want to be certain that a given event satisfies the filter
> you will need to check it inside the callback. For example:
```C#
Action<KeyValuePair<string, object>> callback = (KeyValuePair<string, object> evnt) =>
{
if(predicate(evnt.Key)) // only print out events that satisfy our filter
{
Console.WriteLine("From Listener {0} Received Event {1} with payload {2}", networkListener.Name, evnt.Key, evnt.Value.ToString());
}
};
```

##### Context-based filtering

Some scenarios require advanced filtering based on extended context.
Expand Down

0 comments on commit cfea110

Please sign in to comment.