diff --git a/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs b/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs
index 840b39ce..e1de6f8d 100644
--- a/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs
+++ b/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs
@@ -56,7 +56,7 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOp
#endif
public static ILoggerFactory AddNLog(this ILoggerFactory factory, IConfiguration configuration)
{
- var provider = CreateNLogProvider(configuration);
+ var provider = CreateNLogLoggerProvider(null, configuration, null);
factory.AddProvider(provider);
return factory;
}
@@ -69,7 +69,7 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory, IConfiguration
/// ILoggerFactory for chaining
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory)
{
- return factory.AddNLog(NLogProviderOptions.Default);
+ return factory.AddNLog((NLogProviderOptions)null);
}
///
@@ -80,8 +80,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory)
/// ILoggerFactory for chaining
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfiguration configuration)
{
- // Try adding Singleton-implementation if not already exists
- factory.Services.TryAddEnumerable(ServiceDescriptor.Singleton(s => CreateNLogProvider(configuration)));
+ AddNLogLoggerProvider(factory.Services, configuration, null, CreateNLogLoggerProvider);
return factory;
}
@@ -93,8 +92,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfigurati
/// ILoggerFactory for chaining
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProviderOptions options)
{
- // Try adding Singleton-implementation if not already exists
- factory.Services.TryAddEnumerable(ServiceDescriptor.Singleton(s => new NLogLoggerProvider(options)));
+ AddNLogLoggerProvider(factory.Services, null, options, CreateNLogLoggerProvider);
return factory;
}
@@ -102,12 +100,17 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProvider
/// Enable NLog as logging provider for Microsoft Extension Logging
///
///
- /// New NLog config.
+ /// New NLog config.
///
- public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration config)
+ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration configuration)
{
- builder.AddNLog();
- LogManager.Configuration = config;
+ 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;
}
@@ -119,10 +122,20 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfi
///
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string configFileRelativePath)
{
- builder.AddNLog();
- LogManager.LoadConfiguration(configFileRelativePath);
+ 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(configFileRelativePath);
+ return provider;
+ });
return builder;
}
+
+ private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, NLogProviderOptions options, Func factory)
+ {
+ services.TryAddEnumerable(ServiceDescriptor.Singleton(serviceProvider => factory(serviceProvider, configuration, options)));
+ }
#endif
///
@@ -183,15 +196,18 @@ public static NLogLoggerProvider Configure(this NLogLoggerProvider nlogProvider,
return nlogProvider;
}
- private static NLogLoggerProvider CreateNLogProvider(IConfiguration configuration)
+ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options)
{
- var provider = new NLogLoggerProvider(new NLogProviderOptions());
+ NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogProviderOptions.Default);
+ configuration = configuration ?? (serviceProvider?.GetService(typeof(IConfiguration)) as IConfiguration);
if (configuration != null)
{
ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
- provider.Configure(configuration.GetSection("Logging:NLog"));
+ if (options == null)
+ {
+ provider.Configure(configuration.GetSection("Logging:NLog"));
+ }
}
-
return provider;
}
}
diff --git a/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs b/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs
index 3de275fb..b41e260a 100644
--- a/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs
+++ b/test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs
@@ -60,7 +60,6 @@ public void AddNLog_LogginBuilder_LogInfo_ShouldLogToNLog()
var config = CreateConfigWithMemoryTarget(out var memoryTarget);
// Act
- builder.AddNLog();
builder.AddNLog(config);
var provider = GetLoggerProvider(builder);
var logger = provider.CreateLogger("logger1");