@@ -45,37 +45,38 @@ 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 environment )
61+ {
62+ if ( ! string . IsNullOrEmpty ( basePath ) )
63+ {
64+ if ( ! string . IsNullOrEmpty ( environment ) )
7365 {
74- setupBuilder . LoadConfigurationFromFile ( $ "nlog.{ environment } .config", optional : true ) ;
66+ var nlogConfigEnvFilePath = Path . Combine ( basePath , $ "nlog.{ environment } .config") ;
67+ if ( File . Exists ( nlogConfigEnvFilePath ) )
68+ return nlogConfigEnvFilePath ;
7569 }
7670
77- return setupBuilder . LoadConfigurationFromFile ( ) ; // No effect, if config already loaded
71+ var nlogConfigFilePath = Path . Combine ( basePath , "nlog.config" ) ;
72+ if ( File . Exists ( nlogConfigFilePath ) )
73+ return nlogConfigFilePath ;
7874 }
75+
76+ if ( ! string . IsNullOrEmpty ( environment ) )
77+ return $ "nlog.{ environment } .config";
78+
79+ return null ;
7980 }
8081
8182 private static string ResolveCurrentAppDirectory ( )
@@ -92,11 +93,6 @@ private static string ResolveCurrentAppDirectory()
9293 return currentBasePath ;
9394 }
9495
95- private static bool IsLoggingConfigurationLoaded ( LoggingConfiguration cfg )
96- {
97- return cfg ? . LoggingRules ? . Count > 0 && cfg ? . AllTargets ? . Count > 0 ;
98- }
99-
10096 private static string GetAspNetCoreEnvironment ( string variableName )
10197 {
10298 try
0 commit comments