Skip to content

Commit

Permalink
LoadConfigurationFromAppSettings - Fallback to environment specific n…
Browse files Browse the repository at this point in the history
…log.config when basepath empty
  • Loading branch information
snakefoot committed Nov 20, 2024
1 parent c364a94 commit 27be075
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,38 @@ public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder
// "NLog"-section in appsettings.json has first priority
return setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config)).LoadConfigurationFromSection(config, nlogConfigSection);
}
else

setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config));

var nlogConfigFile = ResolveHostNLogConfigFile(basePath, environment);
if (!string.IsNullOrEmpty(nlogConfigFile))
{
setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config));
return setupBuilder.LoadConfigurationFromFile(nlogConfigFile, optional: true);
}

if (!string.IsNullOrEmpty(basePath))
{
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");
config.Configuration = File.Exists(nlogConfigFilePath) ? new XmlLoggingConfiguration(nlogConfigFilePath, config.LogFactory) : null;
}
});
}
else
{
setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, "nlog.config"), optional: true);
}
}
else if (!string.IsNullOrEmpty(environment))
return setupBuilder.LoadConfigurationFromFile(); // No effect, if config already loaded
}

private static string ResolveHostNLogConfigFile(string basePath, string environment)
{
if (!string.IsNullOrEmpty(basePath))
{
if (!string.IsNullOrEmpty(environment))
{
setupBuilder.LoadConfigurationFromFile($"nlog.{environment}.config", optional: true);
var nlogConfigEnvFilePath = Path.Combine(basePath, $"nlog.{environment}.config");
if (File.Exists(nlogConfigEnvFilePath))
return nlogConfigEnvFilePath;
}

return setupBuilder.LoadConfigurationFromFile(); // No effect, if config already loaded
var nlogConfigFilePath = Path.Combine(basePath, "nlog.config");
if (File.Exists(nlogConfigFilePath))
return nlogConfigFilePath;
}

if (!string.IsNullOrEmpty(environment))
return $"nlog.{environment}.config";

return null;
}

private static string ResolveCurrentAppDirectory()
Expand All @@ -92,11 +93,6 @@ private static string ResolveCurrentAppDirectory()
return currentBasePath;
}

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 27be075

Please sign in to comment.