I originally had code to handle DispatcherUnhandledException
which would log the error and mark the exception as handled
protected override void OnStartup(StartupEventArgs e)
{
Dispatcher.UnhandledException += OnDispatcherUnhandledException;
}
...
void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
// Log Error here
e.Handled = true;
}
I've tried to improve this by covering a wider range of unhandled exceptions
protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += (s, ex) =>
LogUnhandledException((Exception)ex.ExceptionObject,
"AppDomain.CurrentDomain.UnhandledException");
DispatcherUnhandledException += (s, ex) =>
LogUnhandledException(ex.Exception,
"Application.Current.DispatcherUnhandledException");
TaskScheduler.UnobservedTaskException += (s, ex) =>
LogUnhandledException(ex.Exception,
"TaskScheduler.UnobservedTaskException");
}
but I cannot handle the exceptions using this event
private void LogUnhandledException(Exception e, string @event)
{
// Log Error here
e.Handled = true; //Doesn't work
}
How can I handle all types of exceptions here so the code will attempt to continue?