From 145bbf5f525b0a61e905d6d19cd13be0ba9530fa Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Fri, 27 Sep 2019 23:22:31 +0200 Subject: [PATCH] IHostBuilder AddNlog attempt to setup ConfigSettingLayoutRenderer very early --- src/NLog.Web.AspNetCore/AspNetExtensions.cs | 59 ++++++++++++--------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/src/NLog.Web.AspNetCore/AspNetExtensions.cs b/src/NLog.Web.AspNetCore/AspNetExtensions.cs index 72347292..dba81ddb 100644 --- a/src/NLog.Web.AspNetCore/AspNetExtensions.cs +++ b/src/NLog.Web.AspNetCore/AspNetExtensions.cs @@ -112,8 +112,13 @@ public static LogFactory ConfigureNLog(this ILoggingBuilder builder, LoggingConf /// Path to NLog configuration file, e.g. nlog.config. public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string configFileName) { - ConfigureServicesNLog(null, builder.Services, serviceProvider => serviceProvider.GetService()); - LogManager.LoadConfiguration(configFileName); + AddNLogLoggerProvider(builder.Services, null, null, (serviceProvider, config, options) => + { + var provider = CreateNLogLoggerProvider(serviceProvider, config, options); + // Delay initialization of targets until we have loaded config-settings + LogManager.LoadConfiguration(configFileName); + return provider; + }); return builder; } @@ -124,8 +129,13 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string confi /// Config for NLog public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration configuration) { - ConfigureServicesNLog(null, builder.Services, serviceProvider => serviceProvider.GetService()); - LogManager.Configuration = configuration; + AddNLogLoggerProvider(builder.Services, null, null, (serviceProvider, config, options) => + { + var provider = CreateNLogLoggerProvider(serviceProvider, config, options); + // Delay initialization of targets until we have loaded config-settings + LogManager.Configuration = configuration; + return provider; + }); return builder; } @@ -150,7 +160,7 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCo throw new ArgumentNullException(nameof(builder)); } - builder.ConfigureServices(services => { ConfigureServicesNLog(options, services, serviceProvider => serviceProvider.GetService()); }); + builder.ConfigureServices((builderContext, services) => AddNLogLoggerProvider(services, builderContext.Configuration, options, CreateNLogLoggerProvider)); return builder; } @@ -175,32 +185,16 @@ public static IHostBuilder UseNLog(this IHostBuilder builder, NLogAspNetCoreOpti throw new ArgumentNullException(nameof(builder)); } - builder.ConfigureServices((hostbuilder, services) => { ConfigureServicesNLog(options, services, serviceProvider => hostbuilder.Configuration); }); + builder.ConfigureServices((builderContext, services) => AddNLogLoggerProvider(services, builderContext.Configuration, options, CreateNLogLoggerProvider)); return builder; } - private static void ConfigureServicesNLog(NLogAspNetCoreOptions options, IServiceCollection services, Func lookupConfiguration) + private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, NLogAspNetCoreOptions options, Func factory) { ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); - services.Replace(ServiceDescriptor.Singleton(serviceProvider => - { - ServiceLocator.ServiceProvider = serviceProvider; - - var provider = new NLogLoggerProvider(options ?? new NLogProviderOptions()); - var configuration = lookupConfiguration(serviceProvider); - if (configuration != null) - { - ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; - if (options == null) - { - provider.Configure(configuration.GetSection("Logging:NLog")); - } - } - - return provider; - })); + services.Replace(ServiceDescriptor.Singleton(serviceProvider => factory(serviceProvider, configuration, options))); //note: this one is called before services.AddSingleton if ((options ?? NLogAspNetCoreOptions.Default).RegisterHttpContextAccessor) @@ -208,6 +202,23 @@ private static void ConfigureServicesNLog(NLogAspNetCoreOptions options, IServic services.AddHttpContextAccessor(); } } + + private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogAspNetCoreOptions options) + { + ServiceLocator.ServiceProvider = serviceProvider; + + NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogAspNetCoreOptions.Default); + configuration = configuration ?? (serviceProvider?.GetService(typeof(IConfiguration)) as IConfiguration); + if (configuration != null) + { + ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; + if (options == null) + { + provider.Configure(configuration.GetSection("Logging:NLog")); + } + } + return provider; + } #endif } }