forked from sim0n00ps/OF-DL
Add global unhandled exception handler to GUI
This commit is contained in:
parent
6b345ea986
commit
c035fa5721
@ -1,4 +1,5 @@
|
|||||||
using Avalonia;
|
using Avalonia;
|
||||||
|
using Avalonia.Controls.ApplicationLifetimes;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using OF_DL.Helpers;
|
using OF_DL.Helpers;
|
||||||
using OF_DL.Services;
|
using OF_DL.Services;
|
||||||
@ -8,9 +9,13 @@ namespace OF_DL.Gui;
|
|||||||
|
|
||||||
public static class Program
|
public static class Program
|
||||||
{
|
{
|
||||||
|
private static int s_hasProcessedUnhandledException;
|
||||||
|
|
||||||
public static bool HidePrivateInfo { get; private set; }
|
public static bool HidePrivateInfo { get; private set; }
|
||||||
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
// Parse command line arguments
|
// Parse command line arguments
|
||||||
HidePrivateInfo = args.Contains("--hide-private-info", StringComparer.OrdinalIgnoreCase);
|
HidePrivateInfo = args.Contains("--hide-private-info", StringComparer.OrdinalIgnoreCase);
|
||||||
@ -20,6 +25,7 @@ public static class Program
|
|||||||
ServiceProvider tempProvider = services.BuildServiceProvider();
|
ServiceProvider tempProvider = services.BuildServiceProvider();
|
||||||
ILoggingService loggingService = tempProvider.GetRequiredService<ILoggingService>();
|
ILoggingService loggingService = tempProvider.GetRequiredService<ILoggingService>();
|
||||||
|
|
||||||
|
RegisterGlobalExceptionHandlers();
|
||||||
Log.Information("Starting OF DL GUI");
|
Log.Information("Starting OF DL GUI");
|
||||||
|
|
||||||
// Check if running in Docker and print a message
|
// Check if running in Docker and print a message
|
||||||
@ -32,9 +38,69 @@ public static class Program
|
|||||||
BuildAvaloniaApp()
|
BuildAvaloniaApp()
|
||||||
.StartWithClassicDesktopLifetime(args);
|
.StartWithClassicDesktopLifetime(args);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
HandleUnhandledException(ex, "Program.Main", isTerminating: true);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Log.CloseAndFlush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static AppBuilder BuildAvaloniaApp() =>
|
private static AppBuilder BuildAvaloniaApp() =>
|
||||||
AppBuilder.Configure<App>()
|
AppBuilder.Configure<App>()
|
||||||
.UsePlatformDetect()
|
.UsePlatformDetect()
|
||||||
.LogToTrace();
|
.LogToTrace();
|
||||||
|
|
||||||
|
private static void RegisterGlobalExceptionHandlers()
|
||||||
|
{
|
||||||
|
AppDomain.CurrentDomain.UnhandledException += (_, eventArgs) =>
|
||||||
|
{
|
||||||
|
HandleUnhandledException(eventArgs.ExceptionObject as Exception,
|
||||||
|
"AppDomain.CurrentDomain.UnhandledException",
|
||||||
|
eventArgs.IsTerminating);
|
||||||
|
};
|
||||||
|
|
||||||
|
TaskScheduler.UnobservedTaskException += (_, eventArgs) =>
|
||||||
|
{
|
||||||
|
HandleUnhandledException(eventArgs.Exception, "TaskScheduler.UnobservedTaskException",
|
||||||
|
isTerminating: false);
|
||||||
|
eventArgs.SetObserved();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void HandleUnhandledException(Exception? exception, string source, bool isTerminating)
|
||||||
|
{
|
||||||
|
if (Interlocked.Exchange(ref s_hasProcessedUnhandledException, 1) != 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (exception != null)
|
||||||
|
{
|
||||||
|
Log.Fatal(exception, "Unhandled exception from {Source}. Terminating={IsTerminating}",
|
||||||
|
source, isTerminating);
|
||||||
|
Console.WriteLine($"Unhandled exception from {source}: {exception}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.Fatal("Unhandled non-exception object from {Source}. Terminating={IsTerminating}",
|
||||||
|
source, isTerminating);
|
||||||
|
Console.WriteLine($"Unhandled non-exception object from {source}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Log.CloseAndFlush();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isTerminating &&
|
||||||
|
Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
|
||||||
|
{
|
||||||
|
desktopLifetime.Shutdown(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user