From def6572b0ca892e71ed51ded401d787230717b9a Mon Sep 17 00:00:00 2001 From: indep-arg <333053532+indep-arg@users.noreply.github.com> Date: Tue, 27 Oct 2026 11:33:03 -0300 Subject: [PATCH] Log unhandled exceptions to Rapr.log When the app crashed at startup (#479) the log only said "Rapr started", so the cause had to be dug out of the Windows event log. Write any unhandled exception to the trace log before the process goes down. UI-thread exceptions still show the standard WinForms error dialog; they are just logged first. --- Rapr/Program.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Rapr/Program.cs b/Rapr/Program.cs index 18c4dae0..9ef762c9 100644 --- a/Rapr/Program.cs +++ b/Rapr/Program.cs @@ -4,6 +4,7 @@ using System.IO; using System.Reflection; using System.Security; +using System.Threading; using System.Windows.Forms; using Rapr.Lang; @@ -72,6 +73,25 @@ private static void CleanUpOldConfig() } } + internal static void LogUnhandledException(object exception) + { + Trace.TraceError($"Unhandled exception: {exception}"); + } + + private static void OnThreadException(object sender, ThreadExceptionEventArgs e) + { + LogUnhandledException(e.Exception); + + // Same dialog WinForms shows when there is no handler. + using (var dialog = new ThreadExceptionDialog(e.Exception)) + { + if (dialog.ShowDialog() == DialogResult.Abort) + { + Application.Exit(); + } + } + } + /// /// The main entry point for the application. /// @@ -85,6 +105,11 @@ public static void Main() Trace.IndentSize = 4; Trace.Listeners.Add(new TextFileTraceListener()); + // Write crashes to Rapr.log; otherwise a crash during startup leaves nothing to report. + Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); + Application.ThreadException += OnThreadException; + AppDomain.CurrentDomain.UnhandledException += (sender, e) => LogUnhandledException(e.ExceptionObject); + AddEnvironmentPaths(@"C:\Windows\System32\CompatTel"); UpdateManager.CleanUpOldFiles();