Skip to content

Commit

Permalink
Properly flush unhandled/inner exceptions into log file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Soapwood committed Jul 30, 2024
1 parent c7e0779 commit 49231fd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
38 changes: 38 additions & 0 deletions VXMusicDesktop/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public App() : base()

ApplicationVersion = Assembly.GetExecutingAssembly().GetName().Version;

Check warning on line 55 in VXMusicDesktop/App.xaml.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

// Subscribe to global exception handlers
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;
this.DispatcherUnhandledException += DispatcherUnhandledExceptionHandler;

IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
Expand Down Expand Up @@ -174,6 +178,39 @@ public static void ConfigureServices()
Logger = ServiceProvider.GetRequiredService<ILogger<App>>();
}

private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex)
{
LogException(ex, "UNHANDLED EXCEPTION OCCURRED!");
}
else
{
Logger.LogError("UNHANDLED EXCEPTION OCCURRED!");
}

// Optionally flush the logs before exit
if (Logger is IDisposable disposableLogger)
{
disposableLogger.Dispose();
}
}

private void DispatcherUnhandledExceptionHandler(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
LogException(e.Exception, "DISPATCHER UNHANDLED EXCEPTION OCCURRED!");
e.Handled = true;
}

private static void LogException(Exception ex, string message)
{
Logger.LogError(ex, message);
if (ex.InnerException != null)
{
LogException(ex.InnerException, "INNER EXCEPTION:");
}
}

void VXMusicOverlay_Exit(object sender, ExitEventArgs e)
{
try
Expand All @@ -198,6 +235,7 @@ void VXMusicOverlay_Exit(object sender, ExitEventArgs e)
}
protected override void OnExit(ExitEventArgs e)
{
NLog.LogManager.Shutdown();
base.OnExit(e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion VXMusicDesktop/NLog.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
autoReload="true"
throwExceptions="false">

<variable name="defaultLogFormat" value="${time} [${level:uppercase=true}] ${logger} | ${message}"/>
<variable name="defaultLogFormat" value="${time} [${level:uppercase=true}] ${logger} | ${message} ${exception:format=ToString,StackTrace:innerFormat=ToString,StackTrace}"/>

<!-- Define the targets where log messages will be sent -->
<targets>
Expand Down
2 changes: 1 addition & 1 deletion VXMusicDesktop/VXMusicDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageIcon>VXLogo.png</PackageIcon>
<ApplicationIcon>Images\VXLogoIcon.ico</ApplicationIcon>
<RunPostBuildEvent>Always</RunPostBuildEvent>
<Version>0.6.4.21</Version>
<Version>0.6.4.22</Version>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<Title>VXMusicDesktop</Title>
<Authors>VirtualXtensions</Authors>
Expand Down

0 comments on commit 49231fd

Please sign in to comment.