Skip to content

Commit 7a6e1cd

Browse files
committed
LoadConfigurationFromAppSettings - Fallback to environment specific nlog.config when basepath empty
1 parent c364a94 commit 7a6e1cd

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,44 @@ public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder
4545
// "NLog"-section in appsettings.json has first priority
4646
return setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config)).LoadConfigurationFromSection(config, nlogConfigSection);
4747
}
48-
else
48+
49+
setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config));
50+
51+
var nlogConfigFile = ResolveEnvironmentNLogConfigFile(basePath, environment);
52+
if (!string.IsNullOrEmpty(nlogConfigFile))
4953
{
50-
setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config));
54+
return setupBuilder.LoadConfigurationFromFile(nlogConfigFile, optional: true);
55+
}
5156

52-
if (!string.IsNullOrEmpty(basePath))
53-
{
54-
if (!string.IsNullOrEmpty(environment))
55-
{
56-
setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, $"nlog.{environment}.config"), optional: true);
57-
setupBuilder.LoadConfiguration(config =>
58-
{
59-
if (!IsLoggingConfigurationLoaded(config.Configuration))
60-
{
61-
// Fallback when environment-specific NLog config could not load
62-
var nlogConfigFilePath = Path.Combine(basePath, "nlog.config");
63-
config.Configuration = File.Exists(nlogConfigFilePath) ? new XmlLoggingConfiguration(nlogConfigFilePath, config.LogFactory) : null;
64-
}
65-
});
66-
}
67-
else
68-
{
69-
setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, "nlog.config"), optional: true);
70-
}
71-
}
72-
else if (!string.IsNullOrEmpty(environment))
57+
return setupBuilder.LoadConfigurationFromFile(); // No effect, if config already loaded
58+
}
59+
60+
private static string ResolveEnvironmentNLogConfigFile(string basePath, string environmentName)
61+
{
62+
if (!string.IsNullOrWhiteSpace(basePath))
63+
{
64+
if (!string.IsNullOrWhiteSpace(environmentName))
7365
{
74-
setupBuilder.LoadConfigurationFromFile($"nlog.{environment}.config", optional: true);
66+
var nlogConfigEnvFilePath = Path.Combine(basePath, $"nlog.{environmentName}.config");
67+
if (File.Exists(nlogConfigEnvFilePath))
68+
return Path.GetFullPath(nlogConfigEnvFilePath);
69+
nlogConfigEnvFilePath = Path.Combine(basePath, $"NLog.{environmentName}.config");
70+
if (File.Exists(nlogConfigEnvFilePath))
71+
return Path.GetFullPath(nlogConfigEnvFilePath);
7572
}
7673

77-
return setupBuilder.LoadConfigurationFromFile(); // No effect, if config already loaded
74+
var nlogConfigFilePath = Path.Combine(basePath, "nlog.config");
75+
if (File.Exists(nlogConfigFilePath))
76+
return Path.GetFullPath(nlogConfigFilePath);
77+
nlogConfigFilePath = Path.Combine(basePath, "NLog.config");
78+
if (File.Exists(nlogConfigFilePath))
79+
return Path.GetFullPath(nlogConfigFilePath);
7880
}
81+
82+
if (!string.IsNullOrWhiteSpace(environmentName))
83+
return $"nlog.{environmentName}.config";
84+
85+
return null;
7986
}
8087

8188
private static string ResolveCurrentAppDirectory()
@@ -92,11 +99,6 @@ private static string ResolveCurrentAppDirectory()
9299
return currentBasePath;
93100
}
94101

95-
private static bool IsLoggingConfigurationLoaded(LoggingConfiguration cfg)
96-
{
97-
return cfg?.LoggingRules?.Count > 0 && cfg?.AllTargets?.Count > 0;
98-
}
99-
100102
private static string GetAspNetCoreEnvironment(string variableName)
101103
{
102104
try

0 commit comments

Comments
 (0)