|
| 1 | +using System.IO; |
| 2 | +using System.Runtime.ExceptionServices; |
| 3 | +using System.Text; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Windows; |
| 7 | + |
| 8 | +namespace ToastDesk; |
| 9 | + |
| 10 | +public static class CrashReporter |
| 11 | +{ |
| 12 | + private static int isShowingCrash; |
| 13 | + |
| 14 | + public static void Install(System.Windows.Application application) |
| 15 | + { |
| 16 | + application.DispatcherUnhandledException += (_, args) => |
| 17 | + { |
| 18 | + ShowFatalException(args.Exception); |
| 19 | + args.Handled = true; |
| 20 | + application.Shutdown(-1); |
| 21 | + }; |
| 22 | + |
| 23 | + AppDomain.CurrentDomain.UnhandledException += (_, args) => |
| 24 | + { |
| 25 | + var exception = args.ExceptionObject as Exception |
| 26 | + ?? new InvalidOperationException($"Unhandled non-Exception object: {args.ExceptionObject}"); |
| 27 | + ShowFatalException(exception); |
| 28 | + }; |
| 29 | + |
| 30 | + TaskScheduler.UnobservedTaskException += (_, args) => |
| 31 | + { |
| 32 | + ShowFatalException(args.Exception); |
| 33 | + args.SetObserved(); |
| 34 | + application.Dispatcher.InvokeAsync(() => application.Shutdown(-1)); |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + public static string FormatExceptionSummary(Exception exception) |
| 39 | + { |
| 40 | + var exceptionInfo = ExceptionDispatchInfo.Capture(exception).SourceException; |
| 41 | + return $"{exceptionInfo.GetType().FullName} (HRESULT 0x{exceptionInfo.HResult:X8}): {exceptionInfo.Message}"; |
| 42 | + } |
| 43 | + |
| 44 | + private static void ShowFatalException(Exception exception) |
| 45 | + { |
| 46 | + if (Interlocked.Exchange(ref isShowingCrash, 1) == 1) |
| 47 | + { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + try |
| 52 | + { |
| 53 | + WriteCrashLog(exception); |
| 54 | + System.Windows.MessageBox.Show( |
| 55 | + BuildCrashMessage(exception), |
| 56 | + "ToastDesk crashed", |
| 57 | + MessageBoxButton.OK, |
| 58 | + MessageBoxImage.Error, |
| 59 | + MessageBoxResult.OK, |
| 60 | + System.Windows.MessageBoxOptions.DefaultDesktopOnly); |
| 61 | + } |
| 62 | + finally |
| 63 | + { |
| 64 | + Interlocked.Exchange(ref isShowingCrash, 0); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static string BuildCrashMessage(Exception exception) |
| 69 | + { |
| 70 | + var builder = new StringBuilder(); |
| 71 | + builder.AppendLine("ToastDesk hit an unhandled error and must close."); |
| 72 | + builder.AppendLine(); |
| 73 | + builder.AppendLine(FormatExceptionSummary(exception)); |
| 74 | + builder.AppendLine(); |
| 75 | + builder.AppendLine($"Source: {exception.Source ?? "Unknown"}"); |
| 76 | + builder.AppendLine($"Target: {exception.TargetSite?.Name ?? "Unknown"}"); |
| 77 | + builder.AppendLine(); |
| 78 | + builder.AppendLine("Stack trace:"); |
| 79 | + builder.AppendLine(exception.ToString()); |
| 80 | + builder.AppendLine(); |
| 81 | + builder.AppendLine($"A copy was written to: {GetCrashLogPath()}"); |
| 82 | + return builder.ToString(); |
| 83 | + } |
| 84 | + |
| 85 | + private static void WriteCrashLog(Exception exception) |
| 86 | + { |
| 87 | + var logPath = GetCrashLogPath(); |
| 88 | + Directory.CreateDirectory(Path.GetDirectoryName(logPath)!); |
| 89 | + File.WriteAllText( |
| 90 | + logPath, |
| 91 | + $"{DateTimeOffset.Now:O}{Environment.NewLine}{BuildCrashMessage(exception)}"); |
| 92 | + } |
| 93 | + |
| 94 | + private static string GetCrashLogPath() |
| 95 | + { |
| 96 | + return Path.Combine( |
| 97 | + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), |
| 98 | + "ToastDesk", |
| 99 | + "crash-last.log"); |
| 100 | + } |
| 101 | +} |
0 commit comments