Skip to content

Commit 660da18

Browse files
authored
UseNLog includes EnvironmentName when loading NLog config (#714)
1 parent 70bae08 commit 660da18

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

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

+19-12
Original file line numberDiff line numberDiff line change
@@ -55,39 +55,46 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
5555
NLogLoggerProvider provider = serviceProvider.CreateNLogLoggerProvider(hostConfiguration, options, null);
5656

5757
var contentRootPath = hostEnvironment?.ContentRootPath;
58-
if (!string.IsNullOrEmpty(contentRootPath))
58+
if (!string.IsNullOrWhiteSpace(contentRootPath))
5959
{
60-
TryLoadConfigurationFromContentRootPath(provider.LogFactory, contentRootPath);
60+
TryLoadConfigurationFromContentRootPath(provider.LogFactory, contentRootPath, hostEnvironment?.EnvironmentName);
6161
}
6262

6363
return provider;
6464
}
6565

66-
private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactory, string contentRootPath)
66+
private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactory, string contentRootPath, string environmentName)
6767
{
6868
logFactory.Setup().LoadConfiguration(config =>
6969
{
70-
if (!IsLoggingConfigurationLoaded(config.Configuration))
70+
if (IsLoggingConfigurationLoaded(config.Configuration))
71+
return;
72+
73+
if (!string.IsNullOrEmpty(environmentName))
74+
{
75+
var nlogConfig = LoadXmlLoggingConfigurationFromPath(contentRootPath, $"NLog.{environmentName}.config", config.LogFactory) ?? LoadXmlLoggingConfigurationFromPath(contentRootPath, "NLog.config", config.LogFactory);
76+
if (nlogConfig != null)
77+
config.Configuration = nlogConfig;
78+
}
79+
else
7180
{
72-
config.Configuration = config.LogFactory.Configuration;
73-
if (!IsLoggingConfigurationLoaded(config.Configuration))
74-
{
75-
config.Configuration = LoadXmlLoggingConfigurationFromPath(contentRootPath, config.LogFactory);
76-
}
81+
var nlogConfig = LoadXmlLoggingConfigurationFromPath(contentRootPath, "NLog.config", config.LogFactory);
82+
if (nlogConfig != null)
83+
config.Configuration = nlogConfig;
7784
}
7885
});
7986
}
8087

81-
private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath(string contentRootPath, LogFactory logFactory)
88+
private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath(string contentRootPath, string nlogConfigFileName, LogFactory logFactory)
8289
{
83-
var standardPath = System.IO.Path.Combine(contentRootPath, "NLog.config");
90+
var standardPath = System.IO.Path.Combine(contentRootPath, nlogConfigFileName);
8491
if (System.IO.File.Exists(standardPath))
8592
{
8693
return new XmlLoggingConfiguration(standardPath, logFactory);
8794
}
8895
else
8996
{
90-
var lowercasePath = System.IO.Path.Combine(contentRootPath, "nlog.config");
97+
var lowercasePath = System.IO.Path.Combine(contentRootPath, nlogConfigFileName.ToLowerInvariant());
9198
if (System.IO.File.Exists(lowercasePath))
9299
{
93100
return new XmlLoggingConfiguration(lowercasePath, logFactory);

0 commit comments

Comments
 (0)