diff --git a/src/NLog.Web.AspNetCore/AspNetExtensions.cs b/src/NLog.Web.AspNetCore/AspNetExtensions.cs
index 723472922..a79e96df6 100644
--- a/src/NLog.Web.AspNetCore/AspNetExtensions.cs
+++ b/src/NLog.Web.AspNetCore/AspNetExtensions.cs
@@ -112,8 +112,11 @@ 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);
+ ConfigureServicesNLog(null, builder.Services, (s, c) =>
+ {
+ c(s.GetService());
+ LogManager.LoadConfiguration(configFileName); // Delay initialization of targets until we have loaded config-settings
+ });
return builder;
}
@@ -124,8 +127,11 @@ 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;
+ ConfigureServicesNLog(null, builder.Services, (s, c) =>
+ {
+ c(s.GetService());
+ LogManager.Configuration = configuration; // Delay initialization of targets until we have loaded config-settings
+ });
return builder;
}
@@ -150,7 +156,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) => ConfigureServicesNLog(options, services, (s, c) => c(builderContext.Configuration)));
return builder;
}
@@ -175,11 +181,11 @@ 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) => ConfigureServicesNLog(options, services, (s, c) => c(builderContext.Configuration)));
return builder;
}
- private static void ConfigureServicesNLog(NLogAspNetCoreOptions options, IServiceCollection services, Func lookupConfiguration)
+ private static void ConfigureServicesNLog(NLogAspNetCoreOptions options, IServiceCollection services, Action> setupConfiguration)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
@@ -189,15 +195,14 @@ private static void ConfigureServicesNLog(NLogAspNetCoreOptions options, IServic
ServiceLocator.ServiceProvider = serviceProvider;
var provider = new NLogLoggerProvider(options ?? new NLogProviderOptions());
- var configuration = lookupConfiguration(serviceProvider);
- if (configuration != null)
+ setupConfiguration(serviceProvider, configuration =>
{
ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
if (options == null)
{
provider.Configure(configuration.GetSection("Logging:NLog"));
}
- }
+ });
return provider;
}));