Skip to content

Commit

Permalink
Merge pull request #494 from snakefoot/master
Browse files Browse the repository at this point in the history
Added AddNLog with functor to create/initialize LogFactory for NLogLoggingProvider
  • Loading branch information
304NotModified authored Oct 7, 2019
2 parents 1e01477 + 404ab98 commit 0fed91b
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 0fed91b

Please sign in to comment.