Skip to content

Commit

Permalink
Fixed conflict issue between UseNLog and default log providers
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored and 304NotModified committed Oct 19, 2019
1 parent 0428643 commit d5319ca
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,28 @@ public static IHostBuilder UseNLog(this IHostBuilder builder, NLogProviderOption
private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, NLogProviderOptions options, Func<IServiceProvider, IConfiguration, NLogProviderOptions, NLogLoggerProvider> factory)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly);
services.Replace(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(serviceProvider => factory(serviceProvider, configuration, options)));
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(serviceProvider => factory(serviceProvider, configuration, options)));
}

private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options)
{
configuration = SetupConfiguration(serviceProvider, configuration);
NLogLoggerProvider provider = new NLogLoggerProvider(options);
if (configuration != null && options == null)
{
provider.Configure(configuration.GetSection("Logging:NLog"));
}
return provider;
}

private static IConfiguration SetupConfiguration(IServiceProvider serviceProvider, IConfiguration configuration)
{
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;
}
}
}

0 comments on commit d5319ca

Please sign in to comment.