Skip to content

Commit

Permalink
Include ContentRootPath when scanning for NLog.config candidate (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Jan 8, 2022
1 parent 68d615d commit dff8023
Showing 1 changed file with 54 additions and 21 deletions.
75 changes: 54 additions & 21 deletions src/NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using NLog.Web.DependencyInjection;
#if ASP_NET_CORE2
using Microsoft.AspNetCore.Builder;
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
using IHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
#endif
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -60,7 +60,7 @@ public static IServiceProvider SetupNLogServiceLocator(this IServiceProvider ser
/// <param name="configFileRelativePath">relative path to NLog configuration file.</param>
/// <returns>LoggingConfiguration for chaining</returns>
[Obsolete("Use UseNLog() on IHostBuilder / IWebHostBuilder, or AddNLogWeb() on ILoggingBuilder")]
public static LoggingConfiguration ConfigureNLog(this IHostingEnvironment env, string configFileRelativePath)
public static LoggingConfiguration ConfigureNLog(this IHostEnvironment env, string configFileRelativePath)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
Expand Down Expand Up @@ -120,9 +120,9 @@ public static ILoggingBuilder AddNLogWeb(this ILoggingBuilder builder)
/// <param name="options">Options for registration of the NLog LoggingProvider and enabling features.</param>
public static ILoggingBuilder AddNLogWeb(this ILoggingBuilder builder, NLogAspNetCoreOptions options)
{
AddNLogLoggerProvider(builder.Services, null, options, (serviceProvider, config, opt) =>
AddNLogLoggerProvider(builder.Services, null, null, options, (serviceProvider, config, env, opt) =>
{
return CreateNLogLoggerProvider(serviceProvider, config, opt);
return CreateNLogLoggerProvider(serviceProvider, config, env, opt);
});
return builder;
}
Expand All @@ -145,9 +145,9 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string confi
/// <param name="configFileName">Path to NLog configuration file, e.g. nlog.config. </param>
public static ILoggingBuilder AddNLogWeb(this ILoggingBuilder builder, string configFileName)
{
AddNLogLoggerProvider(builder.Services, null, null, (serviceProvider, config, options) =>
AddNLogLoggerProvider(builder.Services, null, null, null, (serviceProvider, config, env, options) =>
{
var provider = CreateNLogLoggerProvider(serviceProvider, config, options);
var provider = CreateNLogLoggerProvider(serviceProvider, config, env, options);
// Delay initialization of targets until we have loaded config-settings
LogManager.LoadConfiguration(configFileName);
return provider;
Expand Down Expand Up @@ -196,10 +196,10 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfi
/// <param name="options">Options for registration of the NLog LoggingProvider and enabling features.</param>
public static ILoggingBuilder AddNLogWeb(this ILoggingBuilder builder, LoggingConfiguration configuration, NLogAspNetCoreOptions options)
{
AddNLogLoggerProvider(builder.Services, null, options, (serviceProvider, config, opt) =>
AddNLogLoggerProvider(builder.Services, null, null, options, (serviceProvider, config, env, opt) =>
{
var logFactory = configuration?.LogFactory ?? LogManager.LogFactory;
var provider = CreateNLogLoggerProvider(serviceProvider, config, opt, logFactory);
var provider = CreateNLogLoggerProvider(serviceProvider, config, env, opt, logFactory);
// Delay initialization of targets until we have loaded config-settings
logFactory.Configuration = configuration;
return provider;
Expand All @@ -225,12 +225,12 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, Func<IServic
/// <param name="factoryBuilder">Initialize NLog LogFactory with NLog LoggingConfiguration.</param>
public static ILoggingBuilder AddNLogWeb(this ILoggingBuilder builder, Func<IServiceProvider, LogFactory> factoryBuilder)
{
AddNLogLoggerProvider(builder.Services, null, null, (serviceProvider, config, options) =>
AddNLogLoggerProvider(builder.Services, null, null, null, (serviceProvider, config, env, 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);
var provider = CreateNLogLoggerProvider(serviceProvider, config, env, options, logFactory);
return provider;
});
return builder;
Expand All @@ -244,10 +244,10 @@ public static ILoggingBuilder AddNLogWeb(this ILoggingBuilder builder, Func<ISer
/// <param name="options">Options for registration of the NLog LoggingProvider and enabling features.</param>
public static ILoggingBuilder AddNLogWeb(this ILoggingBuilder builder, LogFactory logFactory, NLogAspNetCoreOptions options)
{
AddNLogLoggerProvider(builder.Services, null, options, (serviceProvider, config, opt) =>
AddNLogLoggerProvider(builder.Services, null, null, options, (serviceProvider, config, env, opt) =>
{
logFactory = logFactory ?? LogManager.LogFactory;
var provider = CreateNLogLoggerProvider(serviceProvider, config, opt, logFactory);
var provider = CreateNLogLoggerProvider(serviceProvider, config, env, opt, logFactory);
return provider;
});
return builder;
Expand All @@ -273,7 +273,7 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCo
throw new ArgumentNullException(nameof(builder));
}

builder.ConfigureServices((builderContext, services) => AddNLogLoggerProvider(services, builderContext.Configuration, options, CreateNLogLoggerProvider));
builder.ConfigureServices((builderContext, services) => AddNLogLoggerProvider(services, builderContext.Configuration, builderContext.HostingEnvironment as IHostEnvironment, options, CreateNLogLoggerProvider));
return builder;
}

Expand All @@ -297,11 +297,11 @@ public static IHostBuilder UseNLog(this IHostBuilder builder, NLogAspNetCoreOpti
throw new ArgumentNullException(nameof(builder));
}

builder.ConfigureServices((builderContext, services) => AddNLogLoggerProvider(services, builderContext.Configuration, options, CreateNLogLoggerProvider));
builder.ConfigureServices((builderContext, services) => AddNLogLoggerProvider(services, builderContext.Configuration, builderContext.HostingEnvironment as IHostEnvironment, options, CreateNLogLoggerProvider));
return builder;
}

private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, NLogAspNetCoreOptions options, Func<IServiceProvider, IConfiguration, NLogAspNetCoreOptions, NLogLoggerProvider> factory)
private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, IHostEnvironment hostEnvironment, NLogAspNetCoreOptions options, Func<IServiceProvider, IConfiguration, IHostEnvironment, NLogAspNetCoreOptions, NLogLoggerProvider> factory)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
Expand All @@ -311,13 +311,13 @@ private static void AddNLogLoggerProvider(IServiceCollection services, IConfigur
if ((options ?? NLogAspNetCoreOptions.Default).ReplaceLoggerFactory)
{
NLogLoggerProvider singleInstance = null; // Ensure that registration of ILoggerFactory and ILoggerProvider shares the same single instance
sharedFactory = (provider, cfg, opt) => singleInstance ?? (singleInstance = factory(provider, cfg, opt));
sharedFactory = (provider, cfg, env, opt) => singleInstance ?? (singleInstance = factory(provider, cfg, env, opt));

services.AddLogging(builder => builder?.ClearProviders()); // Cleanup the existing LoggerFactory, before replacing it with NLogLoggerFactory
services.Replace(ServiceDescriptor.Singleton<ILoggerFactory, NLogLoggerFactory>(serviceProvider => new NLogLoggerFactory(sharedFactory(serviceProvider, configuration, options))));
services.Replace(ServiceDescriptor.Singleton<ILoggerFactory, NLogLoggerFactory>(serviceProvider => new NLogLoggerFactory(sharedFactory(serviceProvider, configuration, hostEnvironment, options))));
}

services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(serviceProvider => sharedFactory(serviceProvider, configuration, options)));
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, NLogLoggerProvider>(serviceProvider => sharedFactory(serviceProvider, configuration, hostEnvironment, options)));

if ((options ?? NLogAspNetCoreOptions.Default).RemoveLoggerFactoryFilter)
{
Expand All @@ -332,12 +332,12 @@ private static void AddNLogLoggerProvider(IServiceCollection services, IConfigur
}
}

private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogAspNetCoreOptions options)
private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, IHostEnvironment hostEnvironment, NLogAspNetCoreOptions options)
{
return CreateNLogLoggerProvider(serviceProvider, configuration, options, null);
return CreateNLogLoggerProvider(serviceProvider, configuration, hostEnvironment, options, null);
}

private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogAspNetCoreOptions options, NLog.LogFactory logFactory)
private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, IHostEnvironment hostEnvironment, NLogAspNetCoreOptions options, NLog.LogFactory logFactory)
{
NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogAspNetCoreOptions.Default, logFactory ?? LogManager.LogFactory);

Expand All @@ -354,6 +354,12 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
TryLoadConfigurationFromSection(provider, configuration);
}

var contentRootPath = hostEnvironment?.ContentRootPath;
if (!string.IsNullOrWhiteSpace(contentRootPath))
{
TryLoadConfigurationFromContentRootPath(provider.LogFactory, contentRootPath);
}

if (provider.Options.ShutdownOnDispose)
{
provider.LogFactory.AutoShutdown = false;
Expand All @@ -362,6 +368,33 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
return provider;
}

private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactory, string contentRootPath)
{
logFactory.Setup().LoadConfiguration(config =>
{
if (config.Configuration.LoggingRules.Count == 0 && config.Configuration.AllTargets.Count == 0)
{
var standardPath = Path.Combine(contentRootPath, "NLog.config");
if (File.Exists(standardPath))
{
config.Configuration = new XmlLoggingConfiguration(standardPath, config.LogFactory);
}
else
{
var lowercasePath = System.IO.Path.Combine(contentRootPath, "nlog.config");
if (File.Exists(lowercasePath))
{
config.Configuration = new XmlLoggingConfiguration(lowercasePath, config.LogFactory);
}
else
{
config.Configuration = null; // Perform default loading
}
}
}
});
}

private static IConfiguration SetupConfiguration(IServiceProvider serviceProvider, IConfiguration configuration)
{
ServiceLocator.ServiceProvider = serviceProvider;
Expand Down

0 comments on commit dff8023

Please sign in to comment.