Skip to content

Commit 9c8bce2

Browse files
authored
Loading NLog.config from contentRootPath as last fallback (Fix Sonar) (#618)
1 parent 7b21251 commit 9c8bce2

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs

+22-17
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,33 @@ private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactor
7575
config.Configuration = config.LogFactory.Configuration;
7676
if (!IsLoggingConfigurationLoaded(config.Configuration))
7777
{
78-
var standardPath = System.IO.Path.Combine(contentRootPath, "NLog.config");
79-
if (System.IO.File.Exists(standardPath))
80-
{
81-
config.Configuration = new XmlLoggingConfiguration(standardPath, config.LogFactory);
82-
}
83-
else
84-
{
85-
var lowercasePath = System.IO.Path.Combine(contentRootPath, "nlog.config");
86-
if (System.IO.File.Exists(lowercasePath))
87-
{
88-
config.Configuration = new XmlLoggingConfiguration(lowercasePath, config.LogFactory);
89-
}
90-
else
91-
{
92-
config.Configuration = null; // Perform default loading
93-
}
94-
}
78+
config.Configuration = LoadXmlLoggingConfigurationFromPath(contentRootPath, config.LogFactory);
9579
}
9680
}
9781
});
9882
}
9983

84+
private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath(string contentRootPath, LogFactory logFactory)
85+
{
86+
var standardPath = System.IO.Path.Combine(contentRootPath, "NLog.config");
87+
if (System.IO.File.Exists(standardPath))
88+
{
89+
return new XmlLoggingConfiguration(standardPath, logFactory);
90+
}
91+
else
92+
{
93+
var lowercasePath = System.IO.Path.Combine(contentRootPath, "nlog.config");
94+
if (System.IO.File.Exists(lowercasePath))
95+
{
96+
return new XmlLoggingConfiguration(lowercasePath, logFactory);
97+
}
98+
else
99+
{
100+
return null; // Perform default loading
101+
}
102+
}
103+
}
104+
100105
private static bool IsLoggingConfigurationLoaded(LoggingConfiguration cfg)
101106
{
102107
return cfg?.LoggingRules?.Count > 0 && cfg?.AllTargets?.Count > 0;

0 commit comments

Comments
 (0)