@@ -387,15 +387,27 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
387387 provider . LogFactory . ServiceRepository . RegisterService ( typeof ( IServiceProvider ) , serviceProvider ) ;
388388 }
389389
390- if ( configuration != null )
390+ if ( configuration is null || ! TryLoadConfigurationFromSection ( provider , configuration ) )
391391 {
392- TryLoadConfigurationFromSection ( provider , configuration ) ;
393- }
392+ string nlogConfigFile = string . Empty ;
393+ string contentRootPath = hostEnvironment ? . ContentRootPath ;
394+ string environmentName = hostEnvironment ? . EnvironmentName ;
395+ if ( ! string . IsNullOrWhiteSpace ( contentRootPath ) || ! string . IsNullOrWhiteSpace ( environmentName ) )
396+ {
397+ provider . LogFactory . Setup ( ) . LoadConfiguration ( cfg =>
398+ {
399+ if ( ! IsLoggingConfigurationLoaded ( cfg . Configuration ) )
400+ {
401+ nlogConfigFile = ResolveEnvironmentNLogConfigFile ( contentRootPath , environmentName ) ;
402+ cfg . Configuration = null ;
403+ }
404+ } ) ;
405+ }
394406
395- var contentRootPath = hostEnvironment ? . ContentRootPath ;
396- if ( ! string . IsNullOrWhiteSpace ( contentRootPath ) )
397- {
398- TryLoadConfigurationFromContentRootPath ( provider . LogFactory , contentRootPath , hostEnvironment . EnvironmentName ) ;
407+ if ( ! string . IsNullOrEmpty ( nlogConfigFile ) )
408+ {
409+ provider . LogFactory . Setup ( ) . LoadConfigurationFromFile ( nlogConfigFile , optional : true ) ;
410+ }
399411 }
400412
401413 if ( provider . Options . ShutdownOnDispose || ! provider . Options . AutoShutdown )
@@ -406,28 +418,32 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
406418 return provider ;
407419 }
408420
409- private static void TryLoadConfigurationFromContentRootPath ( LogFactory logFactory , string contentRootPath , string environmentName )
421+ private static string ResolveEnvironmentNLogConfigFile ( string basePath , string environmentName )
410422 {
411- logFactory . Setup ( ) . LoadConfiguration ( config =>
423+ if ( ! string . IsNullOrWhiteSpace ( basePath ) )
412424 {
413- if ( IsLoggingConfigurationLoaded ( config . Configuration ) )
414- return ;
415-
416- if ( ! string . IsNullOrEmpty ( environmentName ) )
425+ if ( ! string . IsNullOrWhiteSpace ( environmentName ) )
417426 {
418- var nlogConfig = LoadXmlLoggingConfigurationFromPath ( contentRootPath , $ "NLog.{ environmentName } .config", config . LogFactory ) ??
419- LoadXmlLoggingConfigurationFromPath ( contentRootPath , $ "nlog.{ environmentName } .config", config . LogFactory ) ??
420- LoadXmlLoggingConfigurationFromPath ( contentRootPath , "NLog.config" , config . LogFactory ) ??
421- LoadXmlLoggingConfigurationFromPath ( contentRootPath , "nlog.config" , config . LogFactory ) ;
422- config . Configuration = nlogConfig ;
427+ var nlogConfigEnvFilePath = System . IO . Path . Combine ( basePath , $ "nlog.{ environmentName } .config") ;
428+ if ( System . IO . File . Exists ( nlogConfigEnvFilePath ) )
429+ return System . IO . Path . GetFullPath ( nlogConfigEnvFilePath ) ;
430+ nlogConfigEnvFilePath = System . IO . Path . Combine ( basePath , $ "NLog.{ environmentName } .config") ;
431+ if ( System . IO . File . Exists ( nlogConfigEnvFilePath ) )
432+ return System . IO . Path . GetFullPath ( nlogConfigEnvFilePath ) ;
423433 }
424- else
425- {
426- var nlogConfig = LoadXmlLoggingConfigurationFromPath ( contentRootPath , "NLog.config" , config . LogFactory ) ??
427- LoadXmlLoggingConfigurationFromPath ( contentRootPath , "nlog.config" , config . LogFactory ) ;
428- config . Configuration = nlogConfig ;
429- }
430- } ) ;
434+
435+ var nlogConfigFilePath = System . IO . Path . Combine ( basePath , "nlog.config" ) ;
436+ if ( System . IO . File . Exists ( nlogConfigFilePath ) )
437+ return System . IO . Path . GetFullPath ( nlogConfigFilePath ) ;
438+ nlogConfigFilePath = System . IO . Path . Combine ( basePath , "NLog.config" ) ;
439+ if ( System . IO . File . Exists ( nlogConfigFilePath ) )
440+ return System . IO . Path . GetFullPath ( nlogConfigFilePath ) ;
441+ }
442+
443+ if ( ! string . IsNullOrWhiteSpace ( environmentName ) )
444+ return $ "nlog.{ environmentName } .config";
445+
446+ return null ;
431447 }
432448
433449 private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath ( string contentRootPath , string nlogConfigFileName , LogFactory logFactory )
@@ -451,10 +467,10 @@ private static IConfiguration SetupNLogConfigSettings(IServiceProvider servicePr
451467 return configuration ;
452468 }
453469
454- private static void TryLoadConfigurationFromSection ( NLogLoggerProvider loggerProvider , IConfiguration configuration )
470+ private static bool TryLoadConfigurationFromSection ( NLogLoggerProvider loggerProvider , IConfiguration configuration )
455471 {
456472 if ( string . IsNullOrEmpty ( loggerProvider . Options . LoggingConfigurationSectionName ) )
457- return ;
473+ return false ;
458474
459475 var nlogConfig = configuration . GetSection ( loggerProvider . Options . LoggingConfigurationSectionName ) ;
460476 if ( nlogConfig ? . GetChildren ( ) ? . Any ( ) == true )
@@ -466,10 +482,12 @@ private static void TryLoadConfigurationFromSection(NLogLoggerProvider loggerPro
466482 configBuilder . Configuration = new NLogLoggingConfiguration ( nlogConfig , loggerProvider . LogFactory ) ;
467483 }
468484 } ) ;
485+ return true ;
469486 }
470487 else
471488 {
472489 Common . InternalLogger . Debug ( "Skip loading NLogLoggingConfiguration from empty config section: {0}" , loggerProvider . Options . LoggingConfigurationSectionName ) ;
490+ return false ;
473491 }
474492 }
475493 }
0 commit comments