Question

I know that I can handle the UnobservedTaskException to prevent any unobserved exceptions from terminating my application when the finalizer runs on the object.

However, I'm not sure where or when I should set up the handler for this event. The XML doc on the event itself within the TaskScheduler class says:

/// This AppDomain-wide event provides a mechanism to prevent exception
/// escalation policy (which, by default, terminates the process) from triggering.
/// Each handler is passed a <see cref="T:System.Threading.Tasks.UnobservedTaskExceptionEventArgs"/> 
/// instance, which may be used to examine the exception and to mark it as observed.

Does this mean I only need one handler in the entire AppDomain? And that all unobserved task exceptions will be handled by that?

Or do I need a handler per assembly / per class, and only unobserved exceptions from tasks within that assembly / class will go to that handler?

Was it helpful?

Solution

Per app-domain is all. It's a static event off the Task class which you should just make sure you hook at logical application startup time. So for a console app, just hook it in Main before you start any other work. For ASP.NET apps, just hook it in Global.asax Application_Start. For a Windows service, in OnStart.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top