AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += GlobalCrashHandler;
Dispatcher.UnhandledException += OnDispatcherUnhandledException;
// ...
/// <summary> Global crash handler. </summary>
/// <param name="sender"> Source of the event. </param>
/// <param name="args"> Unhandled exception event information. </param>
private static void GlobalCrashHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception)args.ExceptionObject;
Log.Fatal(e, "Caught by Global Exception catcher. :(");
CloseTheLog();
}
/// <summary> Raises the system. windows. threading. dispatcher unhandled exception event. </summary>
/// <param name="sender"> Source of the event. </param>
/// <param name="e"> Event information to send to registered event handlers. </param>
private static void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
Exception ex = e.Exception;
Log.Fatal(ex, "Caught by Global Dispatcher Exception catcher. :( : {InnerException}", ex.InnerException);
CloseTheLog();
}
private static void CloseTheLog()
{
if (RealDwgHost.UnresolvedFiles.Any())
Log.Error("RealDwg could not find the folllowing Files: {UnresolvedFiles}", RealDwgHost.UnresolvedFiles);
Log.Information("Crashed. Closing tvCADdesktop. Sorry. :(");
Settings.Default.Save();
Log.CloseAndFlush(); // https://github.com/serilog/serilog/wiki/Lifecycle-of-Loggers
}