Skip to content

Commit

Permalink
IHostBuilder UseNLog attempt to setup ConfigSettingLayoutRenderer ver…
Browse files Browse the repository at this point in the history
…y early
  • Loading branch information
snakefoot committed Sep 27, 2019
1 parent 1ed597d commit 7d712e7
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ public static LogFactory ConfigureNLog(this ILoggingBuilder builder, LoggingConf
/// <param name="configFileName">Path to NLog configuration file, e.g. nlog.config. </param>
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string configFileName)
{
ConfigureServicesNLog(null, builder.Services, serviceProvider => serviceProvider.GetService<IConfiguration>());
LogManager.LoadConfiguration(configFileName);
ConfigureServicesNLog(null, builder.Services, (s, c) =>
{
c(s.GetService<IConfiguration>());
LogManager.LoadConfiguration(configFileName); // Delay initialization of targets until we have loaded config-settings
});
return builder;
}

Expand All @@ -124,8 +127,11 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string confi
/// <param name="configuration">Config for NLog</param>
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration configuration)
{
ConfigureServicesNLog(null, builder.Services, serviceProvider => serviceProvider.GetService<IConfiguration>());
LogManager.Configuration = configuration;
ConfigureServicesNLog(null, builder.Services, (s, c) =>
{
c(s.GetService<IConfiguration>());
LogManager.Configuration = configuration; // Delay initialization of targets until we have loaded config-settings
});
return builder;
}

Expand All @@ -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<IConfiguration>()); });
builder.ConfigureServices((builderContext, services) => ConfigureServicesNLog(options, services, (s, c) => c(builderContext.Configuration)));
return builder;
}

Expand All @@ -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<IServiceProvider, IConfiguration> lookupConfiguration)
private static void ConfigureServicesNLog(NLogAspNetCoreOptions options, IServiceCollection services, Action<IServiceProvider, Action<IConfiguration>> setupConfiguration)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
Expand All @@ -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;
}));
Expand Down

0 comments on commit 7d712e7

Please sign in to comment.