From bf9fb5bc83c83b42307e2b2f80db14da7c5b3ae9 Mon Sep 17 00:00:00 2001 From: jbe2277 Date: Sun, 13 Oct 2024 22:18:44 +0200 Subject: [PATCH] Samples: Improve and simplify unhandled exception logging (part 2) --- .../BookLibrary.Library.Presentation/App.xaml.cs | 13 +++++-------- .../InformationManager/Assembler/App.xaml.cs | 14 +++++--------- .../Samples/Writer/Writer.Presentation/App.xaml.cs | 13 +++++-------- 3 files changed, 15 insertions(+), 25 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 7c002886..6711a383 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,9 +71,8 @@ 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 += (_, 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); + AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; + TaskScheduler.UnobservedTaskException += (_, ea) => Log.Default.Warn(ea.Exception, "UnobservedTaskException"); #endif AppConfig appConfig; try @@ -134,12 +133,10 @@ private static void InitializeCultures(AppConfig appConfig) } } - private void HandleException(Exception? ex, string source, bool showError) + private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) { - if (ex is null) return; - Log.App.Error(ex, "Unhandled exception: {0}", source); - - if (!showError) return; + var ex = e.ExceptionObject as Exception ?? throw new InvalidOperationException("Unknown exception object"); + Log.Default.Error(ex, "UnhandledException; IsTerminating={0}", e.IsTerminating); var message = string.Format(CultureInfo.CurrentCulture, Presentation.Properties.Resources.UnknownError, ex); if (MainWindow?.IsVisible == true) diff --git a/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs b/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs index 9a628b0a..37661e0b 100644 --- a/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs +++ b/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs @@ -9,7 +9,6 @@ using System.Waf.Applications; using System.Waf.Applications.Services; using System.Windows; -using System.Windows.Threading; using Waf.InformationManager.Assembler.Properties; using Waf.InformationManager.Common.Applications.Services; @@ -70,9 +69,8 @@ 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 += (_, 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); + AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; + TaskScheduler.UnobservedTaskException += (_, ea) => Log.App.Warn(ea.Exception, "UnobservedTaskException"); #endif AppConfig appConfig; try @@ -132,12 +130,10 @@ private static void InitializeCultures(AppConfig appConfig) } } - private void HandleException(Exception? ex, string source, bool showError) + private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) { - if (ex is null) return; - Log.App.Error(ex, "Unhandled exception: {0}", source); - - if (!showError) return; + var ex = e.ExceptionObject as Exception ?? throw new InvalidOperationException("Unknown exception object"); + Log.App.Error(ex, "UnhandledException; IsTerminating={0}", e.IsTerminating); var message = string.Format(CultureInfo.CurrentCulture, "Unknown application error\n\n{0}", ex); if (MainWindow?.IsVisible == true) 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 34ccceb3..244bae4f 100644 --- a/src/System.Waf/Samples/Writer/Writer.Presentation/App.xaml.cs +++ b/src/System.Waf/Samples/Writer/Writer.Presentation/App.xaml.cs @@ -31,9 +31,8 @@ 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 += (_, 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); + AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; + TaskScheduler.UnobservedTaskException += (_, ea) => Log.Default.Warn(ea.Exception, "UnobservedTaskException"); #endif AppConfig appConfig; try @@ -91,12 +90,10 @@ private static void InitializeCultures(AppConfig appConfig, AppSettings settings } } - private void HandleException(Exception? ex, string source, bool showError) + private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) { - if (ex is null) return; - Log.App.Error(ex, "Unhandled exception: {0}", source); - - if (!showError) return; + var ex = e.ExceptionObject as Exception ?? throw new InvalidOperationException("Unknown exception object"); + Log.Default.Error(ex, "UnhandledException; IsTerminating={0}", e.IsTerminating); var message = string.Format(CultureInfo.CurrentCulture, Presentation.Properties.Resources.UnknownError, ex); if (MainWindow?.IsVisible == true)