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

Introduce AddNLog with NLog-config parameter #344

Merged
merged 2 commits into from
Sep 28, 2019
Merged
Show file tree
Hide file tree
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
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;
}
}
}
62 changes: 52 additions & 10 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,10 +92,50 @@ 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="configuration">New NLog config.</param>
/// <returns></returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration configuration)
{
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;
}

/// <summary>
/// Enable NLog as logging provider for Microsoft Extension Logging
/// </summary>
/// <param name="builder"></param>
/// <param name="configFileRelativePath">relative path to NLog configuration file.</param>
/// <returns></returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string 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 @@ -157,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,8 +60,7 @@ public void AddNLog_LogginBuilder_LogInfo_ShouldLogToNLog()
var config = CreateConfigWithMemoryTarget(out var memoryTarget);

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

Expand All @@ -84,7 +83,7 @@ internal class LoggingBuilderStub : ILoggingBuilder
public IServiceCollection Services { get; set; } = new ServiceCollection();
}
#endif

private static void AssertSingleMessage(MemoryTarget memoryTarget, string expectedMessage)
{
Assert.Equal(1, memoryTarget.Logs.Count);
Expand Down