Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improve doc
  • Loading branch information
reyang committed Feb 28, 2021
commit dbd4f44a3172f050c3fdd2d4fc7a30f1cc7135e7
4 changes: 4 additions & 0 deletions docs/trace/exception-handling/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ private static void Func()

private static void UnhandledExceptionHandler(object source, UnhandledExceptionEventArgs args)
{
var ex = (Exception)args.ExceptionObject;

var activity = Activity.Current;

while (activity != null)
{
activity.SetTag("exception.type", $"UnhandledException<{ex.GetType().Name}>");
activity.SetTag("exception.message", ex.Message);
activity.Dispose();
activity = activity.Parent;
}
Expand Down
37 changes: 9 additions & 28 deletions docs/trace/exception-handling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,13 @@ presence of a debugger:
* In case a postmortem debugger is configured, the postmortem debugger will be
activited and normally it will collect a crash dump.

Handling _unhandled exception_ is a very dangerous thing since the handler
itself could introduce exception, which would result in an unrecoverable
situation similar to [triple fault](https://en.wikipedia.org/wiki/Triple_fault).

In a non-production environment, it might be useful to automatically capture the
unhandled exceptions, travel through the unfinished activities and export them
for troubleshooting. Here goes one possible way of doing this:

<!-- markdownlint-disable MD013 -->
```csharp
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;
}

static void UnhandledExceptionHandler(object source, UnhandledExceptionEventArgs args)
{
var activity = Activity.Current;
It might be useful to automatically capture the unhandled exceptions, travel
through the unfinished activities and export them for troubleshooting. One
possible way of doing this by using
[AppDomain.UnhandledException](https://docs.microsoft.com/dotnet/api/system.appdomain.unhandledexception)
can be found [here](./Program.cs).

while (activity != null)
{
activity.Dispose();
activity = activity.Parent;
}
}
```
<!-- markdownlint-enable MD013 -->

A complete example can be found [here](./Program.cs).
Note: _handling unhandled exception is a very dangerous thing since the handler
itself could introduce exception, which would result in an unrecoverable
situation similar to [triple
fault](https://en.wikipedia.org/wiki/Triple_fault)_.