From 21fe0da27853c429b8fbc0942e692f343e898d37 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Thu, 27 Oct 2022 20:46:49 +0200 Subject: [PATCH] LoadConfigurationFromSection should RegisterConfigSettings without configuration assign (#627) --- .../Config/SetupBuilderExtensions.cs | 2 +- .../Config/SetupExtensionsBuilderExtensions.cs | 15 ++++----------- .../Internal/RegisterNLogLoggingProvider.cs | 2 +- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/NLog.Extensions.Logging/Config/SetupBuilderExtensions.cs b/src/NLog.Extensions.Logging/Config/SetupBuilderExtensions.cs index c327c056..8ef2b543 100644 --- a/src/NLog.Extensions.Logging/Config/SetupBuilderExtensions.cs +++ b/src/NLog.Extensions.Logging/Config/SetupBuilderExtensions.cs @@ -13,7 +13,7 @@ public static class SetupBuilderExtensions /// public static ISetupBuilder LoadConfigurationFromSection(this ISetupBuilder setupBuilder, Microsoft.Extensions.Configuration.IConfiguration configuration, string configSection = "NLog") { - setupBuilder.SetupExtensions(ext => ext.RegisterExtensionsLogging(configuration)); + setupBuilder.SetupExtensions(ext => ext.RegisterConfigSettings(ConfigSettingLayoutRenderer.DefaultConfiguration ?? configuration)); if (!string.IsNullOrEmpty(configSection)) { var nlogConfig = configuration.GetSection(configSection); diff --git a/src/NLog.Extensions.Logging/Config/SetupExtensionsBuilderExtensions.cs b/src/NLog.Extensions.Logging/Config/SetupExtensionsBuilderExtensions.cs index 7476831b..7a94b0fe 100644 --- a/src/NLog.Extensions.Logging/Config/SetupExtensionsBuilderExtensions.cs +++ b/src/NLog.Extensions.Logging/Config/SetupExtensionsBuilderExtensions.cs @@ -13,17 +13,10 @@ public static class SetupExtensionsBuilderExtensions /// public static ISetupExtensionsBuilder RegisterConfigSettings(this ISetupExtensionsBuilder setupBuilder, IConfiguration configuration) { - ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; - return setupBuilder.RegisterExtensionsLogging(configuration); - } - - internal static ISetupExtensionsBuilder RegisterExtensionsLogging(this ISetupExtensionsBuilder setupBuilder, IConfiguration configuration) - { - if (ConfigSettingLayoutRenderer.DefaultConfiguration is null) - { - ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; - } - return setupBuilder.RegisterLayoutRenderer("configsetting").RegisterLayoutRenderer("MicrosoftConsoleLayout").RegisterLayout("MicrosoftConsoleJsonLayout"); + ConfigSettingLayoutRenderer.DefaultConfiguration = configuration ?? ConfigSettingLayoutRenderer.DefaultConfiguration; + return setupBuilder.RegisterLayoutRenderer("configsetting") + .RegisterLayoutRenderer("MicrosoftConsoleLayout") + .RegisterLayout("MicrosoftConsoleJsonLayout"); } } } diff --git a/src/NLog.Extensions.Logging/Internal/RegisterNLogLoggingProvider.cs b/src/NLog.Extensions.Logging/Internal/RegisterNLogLoggingProvider.cs index df7bccf9..d9009f30 100644 --- a/src/NLog.Extensions.Logging/Internal/RegisterNLogLoggingProvider.cs +++ b/src/NLog.Extensions.Logging/Internal/RegisterNLogLoggingProvider.cs @@ -90,7 +90,7 @@ internal static NLogLoggerProvider CreateNLogLoggerProvider(this IServiceProvide internal static IConfiguration SetupNLogConfigSettings(this IServiceProvider serviceProvider, IConfiguration configuration, LogFactory logFactory) { - configuration = configuration ?? (serviceProvider?.GetService(typeof(IConfiguration)) as IConfiguration) ?? ConfigSettingLayoutRenderer.DefaultConfiguration; + configuration = configuration ?? (serviceProvider?.GetService(typeof(IConfiguration)) as IConfiguration); logFactory.Setup().SetupExtensions(ext => ext.RegisterConfigSettings(configuration)); return configuration; }