gkinsman
2/21/2017 - 6:24 AM

UnhandledExceptionLogger

UnhandledExceptionLogger

public static class UnhandledExceptionLogger
    {
        public static void Install()
        {
            var log = Log.ForContext(typeof(UnhandledExceptionLogger));

            AppDomain.CurrentDomain.UnhandledException += (s, e) =>
            {
                log.Fatal(e.ExceptionObject as Exception, "Unhandled exception caught at AppDomain boundary (terminating: {IsTerminating})", e.IsTerminating);
            };

            TaskScheduler.UnobservedTaskException += (s, e) =>
            {
                e.SetObserved();
                log.Error(e.Exception, "Unobserved task exception detected, set observed");
            };

            Application.Current.DispatcherUnhandledException += (s, e) =>
            {
                Log.Error(e.Exception, "Unhandled exception on UI thread", e.Exception.Message);
                e.Handled = true;
            };
        }
    }