Skip to content

Commit

Permalink
Configure NLogProviderOptions from HostBuilder Configuration (code st…
Browse files Browse the repository at this point in the history
…yle) (#582)
  • Loading branch information
snakefoot authored Mar 25, 2022
1 parent 3de0c5c commit 89c4865
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ private static void AddNLogLoggerProvider(IServiceCollection services, IConfigur
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly);
services.TryAddNLogLoggingProvider((svc, addlogging) => svc.AddLogging(addlogging), hostConfiguration, options ?? new NLogProviderOptions(), (provider, cfg, opt) => factory(provider, cfg, hostEnvironment, opt));
services.TryAddNLogLoggingProvider((svc, addlogging) => svc.AddLogging(addlogging), hostConfiguration, options, (provider, cfg, opt) => factory(provider, cfg, hostEnvironment, opt));
}

private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration hostConfiguration, IHostEnvironment hostEnvironment, NLogProviderOptions options)
{
NLogLoggerProvider provider = RegisterNLogLoggingProvider.CreateNLogLoggerProvider(serviceProvider, hostConfiguration, options, null);
NLogLoggerProvider provider = serviceProvider.CreateNLogLoggerProvider(hostConfiguration, options, null);

var contentRootPath = hostEnvironment?.ContentRootPath;
if (!string.IsNullOrEmpty(contentRootPath))
Expand Down
12 changes: 7 additions & 5 deletions src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, Func<IServic
{
AddNLogLoggerProvider(builder, null, null, (serviceProvider, config, options) =>
{
RegisterNLogLoggingProvider.SetupConfiguration(serviceProvider, config);
serviceProvider.SetupNLogConfigSettings(config);

// Delay initialization of targets until we have loaded config-settings
var logFactory = factoryBuilder(serviceProvider);
Expand All @@ -180,7 +180,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, Func<IServic

private static void AddNLogLoggerProvider(ILoggingBuilder builder, IConfiguration hostConfiguration, NLogProviderOptions options, Func<IServiceProvider, IConfiguration, NLogProviderOptions, NLogLoggerProvider> factory)
{
builder.Services.TryAddNLogLoggingProvider((svc, addlogging) => addlogging(builder), hostConfiguration, options ?? NLogProviderOptions.Default, factory);
builder.Services.TryAddNLogLoggingProvider((svc, addlogging) => addlogging(builder), hostConfiguration, options, factory);
}
#endif

Expand Down Expand Up @@ -234,8 +234,10 @@ public static NLogLoggerProvider Configure(this NLogLoggerProvider nlogProvider,
/// <returns></returns>
public static NLogProviderOptions Configure(this NLogProviderOptions options, IConfigurationSection configurationSection)
{
if (options == null || configurationSection == null || !(configurationSection.GetChildren()?.Any() ?? false))
return options ?? NLogProviderOptions.Default;
options = options ?? NLogProviderOptions.Default;

if (configurationSection == null || !(configurationSection.GetChildren()?.Any() ?? false))
return options;

var configProps = options.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.SetMethod?.IsPublic == true).ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase);
foreach (var configValue in configurationSection.GetChildren())
Expand Down Expand Up @@ -263,7 +265,7 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv

private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration hostConfiguration, NLogProviderOptions options, LogFactory logFactory)
{
return RegisterNLogLoggingProvider.CreateNLogLoggerProvider(serviceProvider, hostConfiguration, options, logFactory);
return serviceProvider.CreateNLogLoggerProvider(hostConfiguration, options, logFactory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ internal static void TryAddNLogLoggingProvider(this IServiceCollection services,
{
var sharedFactory = factory;

if (hostConfiguration != null)
{
options.Configure(hostConfiguration.GetSection("Logging:NLog"));
}
options = options.Configure(hostConfiguration?.GetSection("Logging:NLog"));

if (options.ReplaceLoggerFactory)
{
Expand Down Expand Up @@ -62,11 +59,11 @@ internal static void TryLoadConfigurationFromSection(this NLogLoggerProvider log
}
}

internal static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration hostConfiguration, NLogProviderOptions options, LogFactory logFactory)
internal static NLogLoggerProvider CreateNLogLoggerProvider(this IServiceProvider serviceProvider, IConfiguration hostConfiguration, NLogProviderOptions options, LogFactory logFactory)
{
var provider = new NLogLoggerProvider(options, logFactory);

var configuration = SetupConfiguration(serviceProvider, hostConfiguration);
var configuration = serviceProvider.SetupNLogConfigSettings(hostConfiguration);

if (configuration != null && (!ReferenceEquals(configuration, hostConfiguration) || options == null))
{
Expand All @@ -86,7 +83,7 @@ internal static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider ser
return provider;
}

internal static IConfiguration SetupConfiguration(IServiceProvider serviceProvider, IConfiguration configuration)
internal static IConfiguration SetupNLogConfigSettings(this IServiceProvider serviceProvider, IConfiguration configuration)
{
configuration = configuration ?? (serviceProvider?.GetService(typeof(IConfiguration)) as IConfiguration);
if (configuration != null)
Expand Down

0 comments on commit 89c4865

Please sign in to comment.