Skip to content

Commit

Permalink
Include ContentRootPath when scanning for NLog.config candidate (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Jan 9, 2022
1 parent 357ff92 commit 20f96b5
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using Microsoft.Extensions.Hosting;
using NLog.Config;
using NLog.Extensions.Logging;
#if NETSTANDARD2_0
using IHostEnvironment = Microsoft.Extensions.Hosting.IHostingEnvironment;
#endif

namespace NLog.Extensions.Hosting
{
Expand Down Expand Up @@ -35,18 +38,18 @@ 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));
builder.ConfigureServices((builderContext, services) => AddNLogLoggerProvider(services, builderContext.Configuration, builderContext.HostingEnvironment, options, CreateNLogLoggerProvider));
return builder;
}

private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, NLogProviderOptions options, Func<IServiceProvider, IConfiguration, NLogProviderOptions, NLogLoggerProvider> factory)
private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, IHostEnvironment hostEnvironment, NLogProviderOptions options, Func<IServiceProvider, IConfiguration, IHostEnvironment, NLogProviderOptions, NLogLoggerProvider> factory)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly);
LogManager.AddHiddenAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly);
services.TryAddNLogLoggingProvider((svc, addlogging) => svc.AddLogging(addlogging), configuration, options, factory);
services.TryAddNLogLoggingProvider((svc, addlogging) => svc.AddLogging(addlogging), configuration, options, (provider, cfg, opt) => factory(provider, cfg, hostEnvironment, opt));
}

private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options)
private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, IHostEnvironment hostEnvironment, NLogProviderOptions options)
{
NLogLoggerProvider provider = new NLogLoggerProvider(options);

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

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

return provider;
}

Expand All @@ -75,5 +84,32 @@ private static IConfiguration SetupConfiguration(IServiceProvider serviceProvide
}
return configuration;
}

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 = System.IO.Path.Combine(contentRootPath, "NLog.config");
if (System.IO.File.Exists(standardPath))
{
config.Configuration = new XmlLoggingConfiguration(standardPath, config.LogFactory);
}
else
{
var lowercasePath = System.IO.Path.Combine(contentRootPath, "nlog.config");
if (System.IO.File.Exists(lowercasePath))
{
config.Configuration = new XmlLoggingConfiguration(lowercasePath, config.LogFactory);
}
else
{
config.Configuration = null; // Perform default loading
}
}
}
});
}
}
}

0 comments on commit 20f96b5

Please sign in to comment.