Skip to content

Commit

Permalink
Fixed AddNLog with NLog-config parameter to initialize ConfigSettingL…
Browse files Browse the repository at this point in the history
…ayoutRenderer first
  • Loading branch information
snakefoot committed Sep 27, 2019
1 parent f42beeb commit 4d82408
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 38 deletions.
41 changes: 20 additions & 21 deletions src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -36,30 +37,28 @@ public static IHostBuilder UseNLog(this IHostBuilder builder)
public static IHostBuilder UseNLog(this IHostBuilder builder, NLogProviderOptions options)
{
if (builder == null) throw new ArgumentNullException(nameof(builder));
builder.ConfigureServices((builderContext, services) => AddNLogLoggerProvider(services, builderContext.Configuration, options, CreateNLogLoggerProvider));
return builder;
}

builder.ConfigureServices((hostbuilder, services) =>
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(ConfigureExtensions).GetTypeInfo()
.Assembly);

if (hostbuilder.Configuration != null)
ConfigSettingLayoutRenderer.DefaultConfiguration = hostbuilder.Configuration;
private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, NLogProviderOptions options, Func<IServiceProvider, IConfiguration, NLogProviderOptions, NLogLoggerProvider> factory)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly);
services.Replace(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(serviceProvider => factory(serviceProvider, configuration, options)));
}

// Try adding Singleton-implementation if not already exists
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(serviceProvider =>
private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options)
{
NLogLoggerProvider provider = new NLogLoggerProvider(options);
if (configuration != null)
{
ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
if (options == null)
{
var provider = new NLogLoggerProvider(options ?? new NLogProviderOptions());
if (hostbuilder.Configuration != null)
{
ConfigSettingLayoutRenderer.DefaultConfiguration = hostbuilder.Configuration;
if (options == null)
provider.Configure(hostbuilder.Configuration.GetSection("Logging:NLog"));
}
return provider;
}));
});

return builder;
provider.Configure(configuration.GetSection("Logging:NLog"));
}
}
return provider;
}
}
}
48 changes: 32 additions & 16 deletions src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOp
#endif
public static ILoggerFactory AddNLog(this ILoggerFactory factory, IConfiguration configuration)
{
var provider = CreateNLogProvider(configuration);
var provider = CreateNLogLoggerProvider(null, configuration, null);
factory.AddProvider(provider);
return factory;
}
Expand All @@ -69,7 +69,7 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory, IConfiguration
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory)
{
return factory.AddNLog(NLogProviderOptions.Default);
return factory.AddNLog((NLogProviderOptions)null);
}

/// <summary>
Expand All @@ -80,8 +80,7 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory)
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfiguration configuration)
{
// Try adding Singleton-implementation if not already exists
factory.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(s => CreateNLogProvider(configuration)));
AddNLogLoggerProvider(factory.Services, configuration, null, CreateNLogLoggerProvider);
return factory;
}

Expand All @@ -93,21 +92,25 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfigurati
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProviderOptions options)
{
// Try adding Singleton-implementation if not already exists
factory.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(s => new NLogLoggerProvider(options)));
AddNLogLoggerProvider(factory.Services, null, options, CreateNLogLoggerProvider);
return factory;
}

/// <summary>
/// Enable NLog as logging provider for Microsoft Extension Logging
/// </summary>
/// <param name="builder"></param>
/// <param name="config">New NLog config.</param>
/// <param name="configuration">New NLog config.</param>
/// <returns></returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration config)
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration configuration)
{
builder.AddNLog();
LogManager.Configuration = config;
AddNLogLoggerProvider(builder.Services, null, null, (serviceProvider, config, options) =>
{
var provider = CreateNLogLoggerProvider(serviceProvider, config, options);
// Delay initialization of targets until we have loaded config-settings
LogManager.Configuration = configuration;
return provider;
});
return builder;
}

Expand All @@ -119,10 +122,20 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfi
/// <returns></returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string configFileRelativePath)
{
builder.AddNLog();
LogManager.LoadConfiguration(configFileRelativePath);
AddNLogLoggerProvider(builder.Services, null, null, (serviceProvider, config, options) =>
{
var provider = CreateNLogLoggerProvider(serviceProvider, config, options);
// Delay initialization of targets until we have loaded config-settings
LogManager.LoadConfiguration(configFileRelativePath);
return provider;
});
return builder;
}

private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, NLogProviderOptions options, Func<IServiceProvider, IConfiguration, NLogProviderOptions, NLogLoggerProvider> factory)
{
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(serviceProvider => factory(serviceProvider, configuration, options)));
}
#endif

/// <summary>
Expand Down Expand Up @@ -183,15 +196,18 @@ public static NLogLoggerProvider Configure(this NLogLoggerProvider nlogProvider,
return nlogProvider;
}

private static NLogLoggerProvider CreateNLogProvider(IConfiguration configuration)
private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options)
{
var provider = new NLogLoggerProvider(new NLogProviderOptions());
NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogProviderOptions.Default);
configuration = configuration ?? (serviceProvider?.GetService(typeof(IConfiguration)) as IConfiguration);
if (configuration != null)
{
ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
provider.Configure(configuration.GetSection("Logging:NLog"));
if (options == null)
{
provider.Configure(configuration.GetSection("Logging:NLog"));
}
}

return provider;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public void AddNLog_LogginBuilder_LogInfo_ShouldLogToNLog()
var config = CreateConfigWithMemoryTarget(out var memoryTarget);

// Act
builder.AddNLog();
builder.AddNLog(config);
var provider = GetLoggerProvider(builder);
var logger = provider.CreateLogger("logger1");
Expand Down

0 comments on commit 4d82408

Please sign in to comment.