Skip to content

Commit

Permalink
Fixed AddNLog with NLog-config parameter to initialize ConfigSettingL…
Browse files Browse the repository at this point in the history
…ayoutRenderer first
  • Loading branch information
snakefoot committed Sep 27, 2019
1 parent f42beeb commit 86c43b4
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -69,7 +69,7 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory, IConfiguration
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory)
{
return factory.AddNLog(NLogProviderOptions.Default);
return factory.AddNLog((NLogProviderOptions)null);
}

/// <summary>
Expand All @@ -80,8 +80,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory)
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfiguration configuration)
{
// Try adding Singleton-implementation if not already exists
factory.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(s => CreateNLogProvider(configuration)));
AddNLogLoggerProvider(factory.Services, configuration, null, CreateNLogLoggerProvider);
return factory;
}

Expand All @@ -93,21 +92,25 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfigurati
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProviderOptions options)
{
// Try adding Singleton-implementation if not already exists
factory.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(s => new NLogLoggerProvider(options)));
AddNLogLoggerProvider(factory.Services, null, options, CreateNLogLoggerProvider);
return factory;
}

/// <summary>
/// Enable NLog as logging provider for Microsoft Extension Logging
/// </summary>
/// <param name="builder"></param>
/// <param name="config">New NLog config.</param>
/// <param name="configuration">New NLog config.</param>
/// <returns></returns>
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;
}

Expand All @@ -119,10 +122,20 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfi
/// <returns></returns>
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<IServiceProvider, IConfiguration, NLogProviderOptions, NLogLoggerProvider> factory)
{
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(serviceProvider => factory(serviceProvider, configuration, options)));
}
#endif

/// <summary>
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 86c43b4

Please sign in to comment.