From d6b232f342a8d675ae68abecaed54c54f41127c2 Mon Sep 17 00:00:00 2001 From: Mbucari <37587114+Mbucari@users.noreply.github.com> Date: Thu, 13 Nov 2025 23:12:57 -0700 Subject: [PATCH] Update Logging and Error Handling - Add Configuration.LoggingEnabled property which gets set as soon as Serilog is configured - Add error handling to InteropFactory --- Source/LibationAvalonia/App.axaml.cs | 1 - Source/LibationAvalonia/Program.cs | 5 ++--- .../Configuration.Logging.cs | 5 ++++- Source/LibationFileManager/InteropFactory.cs | 18 +++++++++++++----- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Source/LibationAvalonia/App.axaml.cs b/Source/LibationAvalonia/App.axaml.cs index b2a7281b..db969cf4 100644 --- a/Source/LibationAvalonia/App.axaml.cs +++ b/Source/LibationAvalonia/App.axaml.cs @@ -222,7 +222,6 @@ public class App : Application // logging is init'd here AppScaffolding.LibationScaffolding.RunPostMigrationScaffolding(AppScaffolding.Variety.Chardonnay, config); - Program.LoggingEnabled = true; } private static void ShowMainWindow(IClassicDesktopStyleApplicationLifetime desktop) diff --git a/Source/LibationAvalonia/Program.cs b/Source/LibationAvalonia/Program.cs index a5ad6097..f9b84f6d 100644 --- a/Source/LibationAvalonia/Program.cs +++ b/Source/LibationAvalonia/Program.cs @@ -12,6 +12,7 @@ using LibationAvalonia.Dialogs; using Avalonia.Threading; using FileManager; using System.Linq; +using System.Reflection; #nullable enable namespace LibationAvalonia @@ -19,7 +20,6 @@ namespace LibationAvalonia static class Program { private static System.Threading.Lock SetupLock { get; } = new(); - internal static bool LoggingEnabled { get; set; } [STAThread] static void Main(string[] args) { @@ -56,7 +56,6 @@ namespace LibationAvalonia // most migrations go in here LibationScaffolding.RunPostConfigMigrations(config); LibationScaffolding.RunPostMigrationScaffolding(Variety.Chardonnay, config); - LoggingEnabled = true; //Start loading the library before loading the main form App.LibraryTask = Task.Run(() => DbContexts.GetLibrary_Flat_NoTracking(includeParents: true)); @@ -106,7 +105,7 @@ namespace LibationAvalonia try { //Try to log the error message before displaying the crash dialog - if (LoggingEnabled) + if (Configuration.Instance.LoggingEnabled) Serilog.Log.Logger.Error(exception, "CRASH"); else LogErrorWithoutSerilog(exception); diff --git a/Source/LibationFileManager/Configuration.Logging.cs b/Source/LibationFileManager/Configuration.Logging.cs index 70fdbc0b..ffbf9f2f 100644 --- a/Source/LibationFileManager/Configuration.Logging.cs +++ b/Source/LibationFileManager/Configuration.Logging.cs @@ -15,6 +15,8 @@ namespace LibationFileManager { private IConfigurationRoot? configuration; + public bool LoggingEnabled { get; private set; } + public void ConfigureLogging() { //pass explicit assemblies to the ConfigurationReaderOptions @@ -40,7 +42,8 @@ namespace LibationFileManager .Destructure.ByTransforming(lp => lp.Path) .Destructure.With() .CreateLogger(); - } + LoggingEnabled = true; + } [Description("The importance of a log event")] public LogEventLevel LogLevel diff --git a/Source/LibationFileManager/InteropFactory.cs b/Source/LibationFileManager/InteropFactory.cs index c3198944..043535d4 100644 --- a/Source/LibationFileManager/InteropFactory.cs +++ b/Source/LibationFileManager/InteropFactory.cs @@ -65,11 +65,19 @@ namespace LibationFileManager AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; - var configAppAssembly = Assembly.LoadFrom(configApp); - var type = typeof(IInteropFunctions); - InteropFunctionsType = configAppAssembly - .GetTypes() - .FirstOrDefault(type.IsAssignableFrom); + try + { + var configAppAssembly = Assembly.LoadFrom(configApp); + var type = typeof(IInteropFunctions); + InteropFunctionsType = configAppAssembly + .GetTypes() + .FirstOrDefault(type.IsAssignableFrom); + } + catch (Exception e) + { + //None of the interop functions are strictly necessary for Libation to run. + Serilog.Log.Logger.Error(e, "Unable to load types from assembly {@configApp}", configApp); + } } private static string? getOSConfigApp() {