I am specifically asking about how to handle unobserved exceptions, not unhandled exceptions. This question is also different from this question which is asking about how to raise fire-and-forget async methods, not about how to handle unobserved exceptions, though they are related.
Unobserved exceptions are the type that are caused by the example shown below, taken from here:
Task op1 = FooAsync();
Task op2 = BarAsync();
await op1;
await op2;
As a rule I try to avoid this situation, but if it should occur I don't want my web site to exit unexpectedly and I don't want these exceptions to be swallowed silently. I at least want to handle them in one place and log them. I don't want to use elmah.
Now, I think that we can do something like this example from here:
TaskScheduler.UnobservedTaskException += (sender, e) =>
{
Console.WriteLine("Saving the day! This exception would have been unobserved: {0}",
e.Exception);
e.SetObserved();
};
But, my questions are: -
- Where to put this, can it go in Application_Start()?
- Will these unobserved exceptions make their way into Application_Error()? If so, how do you identity them or prevent them from turning up in Application_Error.
This question is similar, but there is no satisfactory answer / explanation.