From cef17bff077c23dfe921d64d3a41659222bb6c4f Mon Sep 17 00:00:00 2001 From: jbe2277 Date: Sun, 13 Oct 2024 17:15:56 +0200 Subject: [PATCH] Samples: Improve unhandled exception logging --- .../App.xaml.cs | 25 +++++++++++------- .../InformationManager/Assembler/App.xaml.cs | 25 +++++++++++------- .../Writer/Writer.Presentation/App.xaml.cs | 26 +++++++++++-------- 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Presentation/App.xaml.cs b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Presentation/App.xaml.cs index 3c2ad6ed..7c002886 100644 --- a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Presentation/App.xaml.cs +++ b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Presentation/App.xaml.cs @@ -71,8 +71,9 @@ protected override void OnStartup(StartupEventArgs e) Log.App.Info("{0} {1} is starting; OS: {2}; .NET: {3}", ApplicationInfo.ProductName, ApplicationInfo.Version, Environment.OSVersion, Environment.Version); #if (!DEBUG) - DispatcherUnhandledException += AppDispatcherUnhandledException; - AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException; + DispatcherUnhandledException += (_, ea) => HandleException(ea.Exception, "Dispatcher", true); + AppDomain.CurrentDomain.UnhandledException += (_, ea) => HandleException(ea.ExceptionObject as Exception, "AppDomain", !ea.IsTerminating); + TaskScheduler.UnobservedTaskException += (_, ea) => HandleException(ea.Exception, "TaskScheduler", false); #endif AppConfig appConfig; try @@ -133,17 +134,21 @@ private static void InitializeCultures(AppConfig appConfig) } } - private void AppDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) => HandleException(e.Exception, false); + private void HandleException(Exception? ex, string source, bool showError) + { + if (ex is null) return; + Log.App.Error(ex, "Unhandled exception: {0}", source); - private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) => HandleException(e.ExceptionObject as Exception, e.IsTerminating); + if (!showError) return; - private static void HandleException(Exception? e, bool isTerminating) - { - if (e == null) return; - Log.App.Error(e, "Unhandled exception"); - if (!isTerminating) + var message = string.Format(CultureInfo.CurrentCulture, Presentation.Properties.Resources.UnknownError, ex); + if (MainWindow?.IsVisible == true) + { + MessageBox.Show(message, ApplicationInfo.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); + } + else { - MessageBox.Show(string.Format(CultureInfo.CurrentCulture, Presentation.Properties.Resources.UnknownError, e), ApplicationInfo.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); + MessageBox.Show(message, ApplicationInfo.ProductName, MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification); } } } diff --git a/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs b/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs index 16cd2923..9a628b0a 100644 --- a/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs +++ b/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs @@ -70,8 +70,9 @@ protected override void OnStartup(StartupEventArgs e) Log.App.Info("{0} {1} is starting; OS: {2}; .NET: {3}", ApplicationInfo.ProductName, ApplicationInfo.Version, Environment.OSVersion, Environment.Version); #if (!DEBUG) - DispatcherUnhandledException += AppDispatcherUnhandledException; - AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException; + DispatcherUnhandledException += (_, ea) => HandleException(ea.Exception, "Dispatcher", true); + AppDomain.CurrentDomain.UnhandledException += (_, ea) => HandleException(ea.ExceptionObject as Exception, "AppDomain", !ea.IsTerminating); + TaskScheduler.UnobservedTaskException += (_, ea) => HandleException(ea.Exception, "TaskScheduler", false); #endif AppConfig appConfig; try @@ -131,17 +132,21 @@ private static void InitializeCultures(AppConfig appConfig) } } - private void AppDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) => HandleException(e.Exception, false); + private void HandleException(Exception? ex, string source, bool showError) + { + if (ex is null) return; + Log.App.Error(ex, "Unhandled exception: {0}", source); - private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) => HandleException(e.ExceptionObject as Exception, e.IsTerminating); + if (!showError) return; - private static void HandleException(Exception? e, bool isTerminating) - { - if (e == null) return; - Log.App.Error(e, "Unhandled exception"); - if (!isTerminating) + var message = string.Format(CultureInfo.CurrentCulture, "Unknown application error\n\n{0}", ex); + if (MainWindow?.IsVisible == true) + { + MessageBox.Show(message, ApplicationInfo.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); + } + else { - MessageBox.Show(string.Format(CultureInfo.CurrentCulture, "Unknown application error\n\n{0}", e), ApplicationInfo.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); + MessageBox.Show(message, ApplicationInfo.ProductName, MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification); } } } diff --git a/src/System.Waf/Samples/Writer/Writer.Presentation/App.xaml.cs b/src/System.Waf/Samples/Writer/Writer.Presentation/App.xaml.cs index 97c22744..34ccceb3 100644 --- a/src/System.Waf/Samples/Writer/Writer.Presentation/App.xaml.cs +++ b/src/System.Waf/Samples/Writer/Writer.Presentation/App.xaml.cs @@ -9,7 +9,6 @@ using System.Waf.Applications.Services; using System.Windows; using System.Windows.Markup; -using System.Windows.Threading; using Waf.Writer.Applications.Properties; using Waf.Writer.Applications.ViewModels; using Microsoft.Extensions.Configuration; @@ -32,8 +31,9 @@ protected override void OnStartup(StartupEventArgs e) Log.App.Info("{0} {1} is starting; OS: {2}; .NET: {3}", ApplicationInfo.ProductName, ApplicationInfo.Version, Environment.OSVersion, Environment.Version); #if (!DEBUG) - DispatcherUnhandledException += AppDispatcherUnhandledException; - AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException; + DispatcherUnhandledException += (_, ea) => HandleException(ea.Exception, "Dispatcher", true); + AppDomain.CurrentDomain.UnhandledException += (_, ea) => HandleException(ea.ExceptionObject as Exception, "AppDomain", !ea.IsTerminating); + TaskScheduler.UnobservedTaskException += (_, ea) => HandleException(ea.Exception, "TaskScheduler", false); #endif AppConfig appConfig; try @@ -91,17 +91,21 @@ private static void InitializeCultures(AppConfig appConfig, AppSettings settings } } - private void AppDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) => HandleException(e.Exception, false); + private void HandleException(Exception? ex, string source, bool showError) + { + if (ex is null) return; + Log.App.Error(ex, "Unhandled exception: {0}", source); - private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) => HandleException(e.ExceptionObject as Exception, e.IsTerminating); + if (!showError) return; - private static void HandleException(Exception? e, bool isTerminating) - { - if (e == null) return; - Log.App.Error(e, "Unhandled exception"); - if (!isTerminating) + var message = string.Format(CultureInfo.CurrentCulture, Presentation.Properties.Resources.UnknownError, ex); + if (MainWindow?.IsVisible == true) + { + MessageBox.Show(message, ApplicationInfo.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); + } + else { - MessageBox.Show(string.Format(CultureInfo.CurrentCulture, Presentation.Properties.Resources.UnknownError, e), ApplicationInfo.ProductName, MessageBoxButton.OK, MessageBoxImage.Error); + MessageBox.Show(message, ApplicationInfo.ProductName, MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification); } } }