From 614b05d5ffa950c9384a2ec95feb4e1f030bc3d4 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Tue, 22 Jul 2025 07:57:15 -0600 Subject: [PATCH] Fix serilog dynamic assembly loading issue (#1310) --- Source/FileLiberator/DownloadDecryptBook.cs | 1 + .../Configuration.Logging.cs | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Source/FileLiberator/DownloadDecryptBook.cs b/Source/FileLiberator/DownloadDecryptBook.cs index a349b497..26ffb9b9 100644 --- a/Source/FileLiberator/DownloadDecryptBook.cs +++ b/Source/FileLiberator/DownloadDecryptBook.cs @@ -118,6 +118,7 @@ namespace FileLiberator } catch when (cancellationToken.IsCancellationRequested) { + Serilog.Log.Logger.Information("Download/Decrypt was cancelled. {@Book}", libraryBook.LogFriendly()); return new StatusHandler { "Cancelled" }; } finally diff --git a/Source/LibationFileManager/Configuration.Logging.cs b/Source/LibationFileManager/Configuration.Logging.cs index 595e18d1..ffb36771 100644 --- a/Source/LibationFileManager/Configuration.Logging.cs +++ b/Source/LibationFileManager/Configuration.Logging.cs @@ -5,6 +5,8 @@ using FileManager; using Microsoft.Extensions.Configuration; using Serilog; using Serilog.Events; +using Serilog.Exceptions; +using Serilog.Settings.Configuration; #nullable enable namespace LibationFileManager @@ -15,11 +17,25 @@ namespace LibationFileManager public void ConfigureLogging() { + //pass explicit assemblies to the ConfigurationReaderOptions + //This is a workaround for the issue where serilog will try to load all + //Assemblies starting with "serilog" from the app folder, but it will fail + //if those assemblies are unreferenced. + //This was a problem when migrating from the ZipFile sink to the File sink. + //Upgrading users would still have the ZipFile sink dll in the program + //folder and serilog would try to load it, unsuccessfully. + //https://github.com/serilog/serilog-settings-configuration/issues/406 + var readerOptions = new ConfigurationReaderOptions( + typeof(ILogger).Assembly, // Serilog + typeof(LoggerEnrichmentConfigurationExtensions).Assembly, // Serilog.Exceptions + typeof(ConsoleLoggerConfigurationExtensions).Assembly, // Serilog.Sinks.Console + typeof(FileLoggerConfigurationExtensions).Assembly); // Serilog.Sinks.File + configuration = new ConfigurationBuilder() .AddJsonFile(SettingsFilePath, optional: false, reloadOnChange: true) .Build(); Log.Logger = new LoggerConfiguration() - .ReadFrom.Configuration(configuration) + .ReadFrom.Configuration(configuration, readerOptions) .Destructure.ByTransforming(lp => lp.Path) .Destructure.With() .CreateLogger();