From 404ab9899b0f519ae353f35a5822f580d0bea466 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sun, 6 Oct 2019 20:31:01 +0200 Subject: [PATCH] Added AddNLog with functor to create/initialize LogFactory for NLogLoggingProvider --- src/NLog.Web.AspNetCore/AspNetExtensions.cs | 43 +++++++++++++++++---- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/NLog.Web.AspNetCore/AspNetExtensions.cs b/src/NLog.Web.AspNetCore/AspNetExtensions.cs index f4c089c4..458d2623 100644 --- a/src/NLog.Web.AspNetCore/AspNetExtensions.cs +++ b/src/NLog.Web.AspNetCore/AspNetExtensions.cs @@ -139,6 +139,25 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfi return builder; } + /// + /// Enable NLog as logging provider for Microsoft Extension Logging + /// + /// + /// Initialize NLog LogFactory with NLog LoggingConfiguration. + /// ILoggingBuilder for chaining + public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, Func factoryBuilder) + { + AddNLogLoggerProvider(builder.Services, null, null, (serviceProvider, config, options) => + { + config = SetupConfiguration(serviceProvider, config); + // Delay initialization of targets until we have loaded config-settings + var logFactory = factoryBuilder(serviceProvider); + var provider = CreateNLogLoggerProvider(serviceProvider, config, options, logFactory); + return provider; + }); + return builder; + } + /// /// Use NLog for Dependency Injected loggers. /// @@ -205,19 +224,29 @@ private static void AddNLogLoggerProvider(IServiceCollection services, IConfigur private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogAspNetCoreOptions options) { - ServiceLocator.ServiceProvider = serviceProvider; + return CreateNLogLoggerProvider(serviceProvider, configuration, options, null); + } - NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogAspNetCoreOptions.Default); + private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogAspNetCoreOptions options, NLog.LogFactory logFactory) + { + configuration = SetupConfiguration(serviceProvider, configuration); + NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogAspNetCoreOptions.Default, logFactory ?? LogManager.LogFactory); + if (configuration != null && options == null) + { + provider.Configure(configuration.GetSection("Logging:NLog")); + } + return provider; + } + + private static IConfiguration SetupConfiguration(IServiceProvider serviceProvider, IConfiguration configuration) + { + ServiceLocator.ServiceProvider = serviceProvider; 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; + return configuration; } #endif }