Skip to content

Commit

Permalink
Added AddNLog with functor to create/initialize LogFactory for NLogLo…
Browse files Browse the repository at this point in the history
…ggingProvider
  • Loading branch information
snakefoot committed Oct 6, 2019
1 parent 1e01477 commit 404ab98
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfi
return builder;
}

/// <summary>
/// Enable NLog as logging provider for Microsoft Extension Logging
/// </summary>
/// <param name="builder"></param>
/// <param name="factoryBuilder">Initialize NLog LogFactory with NLog LoggingConfiguration.</param>
/// <returns>ILoggingBuilder for chaining</returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, Func<IServiceProvider, LogFactory> factoryBuilder)
{
AddNLogLoggerProvider(builder.Services, null, null, (serviceProvider, config, options) =>
{
config = SetupConfiguration(serviceProvider, config);
// Delay initialization of targets until we have loaded config-settings
var logFactory = factoryBuilder(serviceProvider);
var provider = CreateNLogLoggerProvider(serviceProvider, config, options, logFactory);
return provider;
});
return builder;
}

/// <summary>
/// Use NLog for Dependency Injected loggers.
/// </summary>
Expand Down Expand Up @@ -205,19 +224,29 @@ private static void AddNLogLoggerProvider(IServiceCollection services, IConfigur

private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogAspNetCoreOptions options)
{
ServiceLocator.ServiceProvider = serviceProvider;
return CreateNLogLoggerProvider(serviceProvider, configuration, options, null);
}

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

private static IConfiguration SetupConfiguration(IServiceProvider serviceProvider, IConfiguration configuration)
{
ServiceLocator.ServiceProvider = serviceProvider;
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;
}
#endif
}
Expand Down

0 comments on commit 404ab98

Please sign in to comment.