Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added AddNLog with functor to create/initialize LogFactory for NLogLoggingProvider #494

Merged
merged 1 commit into from
Oct 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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