@@ -55,39 +55,46 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
55
55
NLogLoggerProvider provider = serviceProvider . CreateNLogLoggerProvider ( hostConfiguration , options , null ) ;
56
56
57
57
var contentRootPath = hostEnvironment ? . ContentRootPath ;
58
- if ( ! string . IsNullOrEmpty ( contentRootPath ) )
58
+ if ( ! string . IsNullOrWhiteSpace ( contentRootPath ) )
59
59
{
60
- TryLoadConfigurationFromContentRootPath ( provider . LogFactory , contentRootPath ) ;
60
+ TryLoadConfigurationFromContentRootPath ( provider . LogFactory , contentRootPath , hostEnvironment ? . EnvironmentName ) ;
61
61
}
62
62
63
63
return provider ;
64
64
}
65
65
66
- private static void TryLoadConfigurationFromContentRootPath ( LogFactory logFactory , string contentRootPath )
66
+ private static void TryLoadConfigurationFromContentRootPath ( LogFactory logFactory , string contentRootPath , string environmentName )
67
67
{
68
68
logFactory . Setup ( ) . LoadConfiguration ( config =>
69
69
{
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
71
80
{
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 ;
77
84
}
78
85
} ) ;
79
86
}
80
87
81
- private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath ( string contentRootPath , LogFactory logFactory )
88
+ private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath ( string contentRootPath , string nlogConfigFileName , LogFactory logFactory )
82
89
{
83
- var standardPath = System . IO . Path . Combine ( contentRootPath , "NLog.config" ) ;
90
+ var standardPath = System . IO . Path . Combine ( contentRootPath , nlogConfigFileName ) ;
84
91
if ( System . IO . File . Exists ( standardPath ) )
85
92
{
86
93
return new XmlLoggingConfiguration ( standardPath , logFactory ) ;
87
94
}
88
95
else
89
96
{
90
- var lowercasePath = System . IO . Path . Combine ( contentRootPath , "nlog.config" ) ;
97
+ var lowercasePath = System . IO . Path . Combine ( contentRootPath , nlogConfigFileName . ToLowerInvariant ( ) ) ;
91
98
if ( System . IO . File . Exists ( lowercasePath ) )
92
99
{
93
100
return new XmlLoggingConfiguration ( lowercasePath , logFactory ) ;
0 commit comments