From 89c4865587e15d701c076c16c3b02964a1af4a74 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Fri, 25 Mar 2022 07:36:15 +0100 Subject: [PATCH] Configure NLogProviderOptions from HostBuilder Configuration (code style) (#582) --- .../Extensions/ConfigureExtensions.cs | 4 ++-- .../Extensions/ConfigureExtensions.cs | 12 +++++++----- .../Internal/RegisterNLogLoggingProvider.cs | 11 ++++------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs b/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs index 52717608..a83cfbb0 100644 --- a/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs +++ b/src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs @@ -46,12 +46,12 @@ private static void AddNLogLoggerProvider(IServiceCollection services, IConfigur { ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly); LogManager.AddHiddenAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly); - services.TryAddNLogLoggingProvider((svc, addlogging) => svc.AddLogging(addlogging), hostConfiguration, options ?? new NLogProviderOptions(), (provider, cfg, opt) => factory(provider, cfg, hostEnvironment, opt)); + services.TryAddNLogLoggingProvider((svc, addlogging) => svc.AddLogging(addlogging), hostConfiguration, options, (provider, cfg, opt) => factory(provider, cfg, hostEnvironment, opt)); } private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration hostConfiguration, IHostEnvironment hostEnvironment, NLogProviderOptions options) { - NLogLoggerProvider provider = RegisterNLogLoggingProvider.CreateNLogLoggerProvider(serviceProvider, hostConfiguration, options, null); + NLogLoggerProvider provider = serviceProvider.CreateNLogLoggerProvider(hostConfiguration, options, null); var contentRootPath = hostEnvironment?.ContentRootPath; if (!string.IsNullOrEmpty(contentRootPath)) diff --git a/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs b/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs index 054ae8af..4679d06f 100644 --- a/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs +++ b/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs @@ -168,7 +168,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, Func { - RegisterNLogLoggingProvider.SetupConfiguration(serviceProvider, config); + serviceProvider.SetupNLogConfigSettings(config); // Delay initialization of targets until we have loaded config-settings var logFactory = factoryBuilder(serviceProvider); @@ -180,7 +180,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, Func factory) { - builder.Services.TryAddNLogLoggingProvider((svc, addlogging) => addlogging(builder), hostConfiguration, options ?? NLogProviderOptions.Default, factory); + builder.Services.TryAddNLogLoggingProvider((svc, addlogging) => addlogging(builder), hostConfiguration, options, factory); } #endif @@ -234,8 +234,10 @@ public static NLogLoggerProvider Configure(this NLogLoggerProvider nlogProvider, /// public static NLogProviderOptions Configure(this NLogProviderOptions options, IConfigurationSection configurationSection) { - if (options == null || configurationSection == null || !(configurationSection.GetChildren()?.Any() ?? false)) - return options ?? NLogProviderOptions.Default; + options = options ?? NLogProviderOptions.Default; + + if (configurationSection == null || !(configurationSection.GetChildren()?.Any() ?? false)) + return options; var configProps = options.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.SetMethod?.IsPublic == true).ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase); foreach (var configValue in configurationSection.GetChildren()) @@ -263,7 +265,7 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration hostConfiguration, NLogProviderOptions options, LogFactory logFactory) { - return RegisterNLogLoggingProvider.CreateNLogLoggerProvider(serviceProvider, hostConfiguration, options, logFactory); + return serviceProvider.CreateNLogLoggerProvider(hostConfiguration, options, logFactory); } } } diff --git a/src/NLog.Extensions.Logging/Internal/RegisterNLogLoggingProvider.cs b/src/NLog.Extensions.Logging/Internal/RegisterNLogLoggingProvider.cs index 0eb80233..fd35c7c6 100644 --- a/src/NLog.Extensions.Logging/Internal/RegisterNLogLoggingProvider.cs +++ b/src/NLog.Extensions.Logging/Internal/RegisterNLogLoggingProvider.cs @@ -16,10 +16,7 @@ internal static void TryAddNLogLoggingProvider(this IServiceCollection services, { var sharedFactory = factory; - if (hostConfiguration != null) - { - options.Configure(hostConfiguration.GetSection("Logging:NLog")); - } + options = options.Configure(hostConfiguration?.GetSection("Logging:NLog")); if (options.ReplaceLoggerFactory) { @@ -62,11 +59,11 @@ internal static void TryLoadConfigurationFromSection(this NLogLoggerProvider log } } - internal static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration hostConfiguration, NLogProviderOptions options, LogFactory logFactory) + internal static NLogLoggerProvider CreateNLogLoggerProvider(this IServiceProvider serviceProvider, IConfiguration hostConfiguration, NLogProviderOptions options, LogFactory logFactory) { var provider = new NLogLoggerProvider(options, logFactory); - var configuration = SetupConfiguration(serviceProvider, hostConfiguration); + var configuration = serviceProvider.SetupNLogConfigSettings(hostConfiguration); if (configuration != null && (!ReferenceEquals(configuration, hostConfiguration) || options == null)) { @@ -86,7 +83,7 @@ internal static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider ser return provider; } - internal static IConfiguration SetupConfiguration(IServiceProvider serviceProvider, IConfiguration configuration) + internal static IConfiguration SetupNLogConfigSettings(this IServiceProvider serviceProvider, IConfiguration configuration) { configuration = configuration ?? (serviceProvider?.GetService(typeof(IConfiguration)) as IConfiguration); if (configuration != null)