diff --git a/NetTally/MainWindow.xaml.cs b/NetTally/MainWindow.xaml.cs index 5df031ef..bf816fdd 100644 --- a/NetTally/MainWindow.xaml.cs +++ b/NetTally/MainWindow.xaml.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; +using System.IO; using System.Linq; using System.Reflection; using System.Threading; @@ -56,6 +57,9 @@ public string MyTitle /// public MainWindow() { + // Set up an event handler for any otherwise unhandled exceptions in the code. + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + InitializeComponent(); // Set tally vars @@ -94,6 +98,29 @@ public MainWindow() MyTitle = $"{product.Product} - {version.InformationalVersion}"; } + /// + /// Unhandled exception handler. If an unhandled exception crashes the program, save + /// the stack trace to a log file. + /// + /// The AppDomain. + /// The details of the unhandled exception. + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + Exception ex = (Exception)e.ExceptionObject; + + // print out the exception stack trace to a log + string output = + $"Message is: {ex.Message}\n\n" + + $"Stack Trace is:\n{ex.StackTrace}\n"; + + string tempFile = Path.GetTempFileName(); + + File.WriteAllText(tempFile, output); + + // Let the user know where the temp file was written. + MessageBox.Show($"Error written to:\n{tempFile}", "Unhandled exception log written", MessageBoxButton.OK, MessageBoxImage.Error); + } + /// /// When the program closes, save the current list of quests. ///