Skip to content

Commit

Permalink
Samples: Improve unhandled exception logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Oct 13, 2024
1 parent 5369efc commit cef17bf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
25 changes: 15 additions & 10 deletions src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
26 changes: 15 additions & 11 deletions src/System.Waf/Samples/Writer/Writer.Presentation/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit cef17bf

Please sign in to comment.