From 97d95627d6b037fc856f53541381e03881f6007f Mon Sep 17 00:00:00 2001 From: neuecc Date: Sun, 7 Jan 2024 21:44:42 +0900 Subject: [PATCH] Avalonia default unhandled exception --- sandbox/AvaloniaApplication1/Program.cs | 2 +- src/R3.Avalonia/AppBuilderR3InitializeExtensions.cs | 13 +++++++++---- src/R3.Avalonia/AvaloniaProviderInitializer.cs | 8 +++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/sandbox/AvaloniaApplication1/Program.cs b/sandbox/AvaloniaApplication1/Program.cs index 46bcce9a..1b7bfcc2 100644 --- a/sandbox/AvaloniaApplication1/Program.cs +++ b/sandbox/AvaloniaApplication1/Program.cs @@ -19,5 +19,5 @@ public static AppBuilder BuildAvaloniaApp() .UsePlatformDetect() .WithInterFont() .LogToTrace() - .UseR3(ex => Logger.Sink?.Log(LogEventLevel.Error, "R3", null, "R3 Unhandled Exception {0}", ex)); // add this line + .UseR3(); // add this line } diff --git a/src/R3.Avalonia/AppBuilderR3InitializeExtensions.cs b/src/R3.Avalonia/AppBuilderR3InitializeExtensions.cs index bc6f1804..18e45899 100644 --- a/src/R3.Avalonia/AppBuilderR3InitializeExtensions.cs +++ b/src/R3.Avalonia/AppBuilderR3InitializeExtensions.cs @@ -5,24 +5,29 @@ namespace Avalonia; // Avalonia namespace public static class AppBuilderR3InitializeExtensions { - public static AppBuilder UseR3(this AppBuilder builder, Action unhandledExceptionHandler) + public static AppBuilder UseR3(this AppBuilder builder) { // need to delay setup, initialize provider(dispatcher) need to determine platform + return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultObservableSystem()); + } + + public static AppBuilder UseR3(this AppBuilder builder, Action unhandledExceptionHandler) + { return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultObservableSystem(unhandledExceptionHandler)); } public static AppBuilder UseR3(this AppBuilder builder, DispatcherPriority priority, Action unhandledExceptionHandler) { - return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultObservableSystem(unhandledExceptionHandler,priority)); + return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultObservableSystem(unhandledExceptionHandler, priority)); } public static AppBuilder UseR3(this AppBuilder builder, int framesPerSecond, Action unhandledExceptionHandler) { - return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultObservableSystem(unhandledExceptionHandler,framesPerSecond)); + return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultObservableSystem(unhandledExceptionHandler, framesPerSecond)); } public static AppBuilder UseR3(this AppBuilder builder, DispatcherPriority priority, int framesPerSecond, Action unhandledExceptionHandler) { - return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultObservableSystem(unhandledExceptionHandler,priority, framesPerSecond)); + return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultObservableSystem(unhandledExceptionHandler, priority, framesPerSecond)); } } diff --git a/src/R3.Avalonia/AvaloniaProviderInitializer.cs b/src/R3.Avalonia/AvaloniaProviderInitializer.cs index 92470e4f..af27cac3 100644 --- a/src/R3.Avalonia/AvaloniaProviderInitializer.cs +++ b/src/R3.Avalonia/AvaloniaProviderInitializer.cs @@ -1,9 +1,15 @@ -using Avalonia.Threading; +using Avalonia.Logging; +using Avalonia.Threading; namespace R3.Avalonia; public static class AvaloniaProviderInitializer { + public static void SetDefaultObservableSystem() + { + SetDefaultObservableSystem(ex => Logger.Sink?.Log(LogEventLevel.Error, "R3", null, "R3 Unhandled Exception {0}", ex)); + } + public static void SetDefaultObservableSystem(Action unhandledExceptionHandler) { ObservableSystem.RegisterUnhandledExceptionHandler(unhandledExceptionHandler);