diff --git a/src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs b/src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs index 4b3b94a9..bb1b379e 100644 --- a/src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs +++ b/src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs @@ -45,37 +45,38 @@ public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder // "NLog"-section in appsettings.json has first priority return setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config)).LoadConfigurationFromSection(config, nlogConfigSection); } - else + + setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config)); + + var nlogConfigFile = ResolveHostNLogConfigFile(basePath, environment); + if (!string.IsNullOrEmpty(nlogConfigFile)) { - setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config)); + return setupBuilder.LoadConfigurationFromFile(nlogConfigFile, optional: true); + } - if (!string.IsNullOrEmpty(basePath)) - { - if (!string.IsNullOrEmpty(environment)) - { - setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, $"nlog.{environment}.config"), optional: true); - setupBuilder.LoadConfiguration(config => - { - if (!IsLoggingConfigurationLoaded(config.Configuration)) - { - // Fallback when environment-specific NLog config could not load - var nlogConfigFilePath = Path.Combine(basePath, "nlog.config"); - config.Configuration = File.Exists(nlogConfigFilePath) ? new XmlLoggingConfiguration(nlogConfigFilePath, config.LogFactory) : null; - } - }); - } - else - { - setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, "nlog.config"), optional: true); - } - } - else if (!string.IsNullOrEmpty(environment)) + return setupBuilder.LoadConfigurationFromFile(); // No effect, if config already loaded + } + + private static string ResolveHostNLogConfigFile(string basePath, string environment) + { + if (!string.IsNullOrEmpty(basePath)) + { + if (!string.IsNullOrEmpty(environment)) { - setupBuilder.LoadConfigurationFromFile($"nlog.{environment}.config", optional: true); + var nlogConfigEnvFilePath = Path.Combine(basePath, $"nlog.{environment}.config"); + if (File.Exists(nlogConfigEnvFilePath)) + return nlogConfigEnvFilePath; } - return setupBuilder.LoadConfigurationFromFile(); // No effect, if config already loaded + var nlogConfigFilePath = Path.Combine(basePath, "nlog.config"); + if (File.Exists(nlogConfigFilePath)) + return nlogConfigFilePath; } + + if (!string.IsNullOrEmpty(environment)) + return $"nlog.{environment}.config"; + + return null; } private static string ResolveCurrentAppDirectory() @@ -92,11 +93,6 @@ private static string ResolveCurrentAppDirectory() return currentBasePath; } - private static bool IsLoggingConfigurationLoaded(LoggingConfiguration cfg) - { - return cfg?.LoggingRules?.Count > 0 && cfg?.AllTargets?.Count > 0; - } - private static string GetAspNetCoreEnvironment(string variableName) { try