Skip to content

Commit

Permalink
Clarify precaution
Browse files Browse the repository at this point in the history
  • Loading branch information
mdh1418 committed Oct 29, 2024
1 parent 64ac663 commit 96fad7b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/core/diagnostics/eventsource-collect-and-view-traces.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ this isn't required. Following is an example `EventListener` implementation that
>
> To mitigate this risk, consider the following precautions:
>
> - **Avoid Complex Operations**: Refrain from performing complex operations within the callback that might acquire additional locks. For example, during some event callbacks, attempting to use File or Console APIs may encounter issues. Instead, you can update an in-memory datastructure or add some information about the event to a queue. If more processing is needed it can be done from a separate thread after the callback has already returned.
> - **Use Queues to Defer Work**: There are a variety of APIs you might want to call in OnEventWritten() that do non-trivial work, for example File IO, Console, or Http. For most events, these APIs work fine, but if you tried to call Console.WriteLine() in an event handler during the initialization of the Console class then it would probably fail. If you don't know whether a given event handler occurs at a time when APIs are safe to call, consider adding some information about the event to an in-memory queue instead. Then, on a separate thread, process items in the queue.
> - **Minimize Lock Duration**: Ensure that any locks acquired within the callback are not held for extended periods.
> - **Use Non-blocking APIs**: Prefer using non-blocking APIs within the callback to avoid potential deadlocks.
> - **Implement Re-entrancy Guard**: Use a re-entrancy guard to prevent infinite recursion. For example:
> - **Implement a Re-entrancy Guard**: Use a re-entrancy guard to prevent infinite recursion. For example:
>
> ```csharp
> [ThreadStatic] private static bool t_insideCallback;
Expand Down

0 comments on commit 96fad7b

Please sign in to comment.