Skip to content

Commit

Permalink
UseNLog includes EnvironmentName when loading NLog config (#1012)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Dec 7, 2023
1 parent 95ae7fd commit 99e9b13
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
29 changes: 18 additions & 11 deletions src/NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
var contentRootPath = hostEnvironment?.ContentRootPath;
if (!string.IsNullOrWhiteSpace(contentRootPath))
{
TryLoadConfigurationFromContentRootPath(provider.LogFactory, contentRootPath);
TryLoadConfigurationFromContentRootPath(provider.LogFactory, contentRootPath, hostEnvironment?.EnvironmentName);
}

if (provider.Options.ShutdownOnDispose || !provider.Options.AutoShutdown)
Expand All @@ -408,31 +408,38 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
return provider;
}

private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactory, string contentRootPath)
private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactory, string contentRootPath, string environmentName)
{
logFactory.Setup().LoadConfiguration(config =>
{
if (!IsLoggingConfigurationLoaded(config.Configuration))
if (IsLoggingConfigurationLoaded(config.Configuration))
return;

if (!string.IsNullOrEmpty(environmentName))
{
config.Configuration = config.LogFactory.Configuration;
if (!IsLoggingConfigurationLoaded(config.Configuration))
{
config.Configuration = LoadXmlLoggingConfigurationFromPath(contentRootPath, config.LogFactory);
}
var nlogConfig = LoadXmlLoggingConfigurationFromPath(contentRootPath, $"NLog.{environmentName}.config", config.LogFactory) ?? LoadXmlLoggingConfigurationFromPath(contentRootPath, "NLog.config", config.LogFactory);
if (nlogConfig != null)
config.Configuration = nlogConfig;
}
else
{
var nlogConfig = LoadXmlLoggingConfigurationFromPath(contentRootPath, "NLog.config", config.LogFactory);
if (nlogConfig != null)
config.Configuration = nlogConfig;
}
});
}

private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath(string contentRootPath, LogFactory logFactory)
private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath(string contentRootPath, string nlogConfigFileName, LogFactory logFactory)
{
var standardPath = System.IO.Path.Combine(contentRootPath, "NLog.config");
var standardPath = System.IO.Path.Combine(contentRootPath, nlogConfigFileName);
if (System.IO.File.Exists(standardPath))
{
return new XmlLoggingConfiguration(standardPath, logFactory);
}
else
{
var lowercasePath = System.IO.Path.Combine(contentRootPath, "nlog.config");
var lowercasePath = System.IO.Path.Combine(contentRootPath, nlogConfigFileName.ToLowerInvariant());
if (System.IO.File.Exists(lowercasePath))
{
return new XmlLoggingConfiguration(lowercasePath, logFactory);
Expand Down
22 changes: 20 additions & 2 deletions src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder
var config = builder.Build();
if (!string.IsNullOrEmpty(nlogConfigSection) && config.GetSection(nlogConfigSection)?.GetChildren().Any() == true)
{
// "NLog"-section in appsettings.json has first priority
return setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config)).LoadConfigurationFromSection(config, nlogConfigSection);
}
else
Expand All @@ -66,9 +67,21 @@ public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder
if (!string.IsNullOrEmpty(environment))
{
setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, $"nlog.{environment}.config"), optional: true);
setupBuilder.LoadConfiguration(config =>
{
if (!IsLoggingConfigurationLoaded(config.Configuration))
{
// Fallback when environment-specific NLog config could not load
var nlogConfigFilePath = Path.Combine(basePath, "nlog.config");
if (File.Exists(nlogConfigFilePath))
config.Configuration = new XmlLoggingConfiguration(nlogConfigFilePath, config.LogFactory);
}
});
}
else
{
setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, "nlog.config"), optional: true);
}

setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, "nlog.config"), optional: true);
}
else if (!string.IsNullOrEmpty(environment))
{
Expand All @@ -79,6 +92,11 @@ public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder
}
}

private static bool IsLoggingConfigurationLoaded(LoggingConfiguration cfg)
{
return cfg?.LoggingRules?.Count > 0 && cfg?.AllTargets?.Count > 0;
}

private static string GetAspNetCoreEnvironment(string variableName)
{
try
Expand Down

0 comments on commit 99e9b13

Please sign in to comment.