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

Improved AddNLog with LoggingConfiguration to handle custom LogFactory #527

Merged
merged 1 commit into from
Mar 18, 2020
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
18 changes: 15 additions & 3 deletions src/NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,23 @@ 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)
{
AddNLogLoggerProvider(builder.Services, null, null, (serviceProvider, config, options) =>
return AddNLog(builder, configuration, null);
}

/// <summary>
/// Configure NLog from API
/// </summary>
/// <param name="builder">The logging builder</param>
/// <param name="configuration">Config for NLog</param>
/// <param name="options">Options for logging to NLog with Dependency Injected loggers</param>
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration configuration, NLogAspNetCoreOptions options)
{
AddNLogLoggerProvider(builder.Services, null, options, (serviceProvider, config, opt) =>
{
var provider = CreateNLogLoggerProvider(serviceProvider, config, options);
var logFactory = configuration?.LogFactory ?? LogManager.LogFactory;
var provider = CreateNLogLoggerProvider(serviceProvider, config, opt, logFactory);
// Delay initialization of targets until we have loaded config-settings
LogManager.Configuration = configuration;
logFactory.Configuration = configuration;
return provider;
});
return builder;
Expand Down