Skip to content

Commit

Permalink
Samples: Improve and simplify unhandled exception logging (part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Oct 13, 2024
1 parent cef17bf commit bf9fb5b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 5 additions & 9 deletions src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 5 additions & 8 deletions src/System.Waf/Samples/Writer/Writer.Presentation/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bf9fb5b

Please sign in to comment.