diff --git a/src/NLog.Web.AspNetCore/AspNetExtensions.cs b/src/NLog.Web.AspNetCore/AspNetExtensions.cs index 2e3d9b27..ef2c0332 100644 --- a/src/NLog.Web.AspNetCore/AspNetExtensions.cs +++ b/src/NLog.Web.AspNetCore/AspNetExtensions.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using System.Linq; using System.Reflection; using NLog.Config; @@ -64,7 +63,7 @@ public static LoggingConfiguration ConfigureNLog(this IHostEnvironment env, stri { ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); - var fileName = Path.Combine(env.ContentRootPath, configFileRelativePath); + var fileName = System.IO.Path.Combine(env.ContentRootPath, configFileRelativePath); LogManager.LoadConfiguration(fileName); return LogManager.Configuration; } @@ -402,29 +401,43 @@ private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactor { logFactory.Setup().LoadConfiguration(config => { - if (config.Configuration.LoggingRules.Count == 0 && config.Configuration.AllTargets.Count == 0) + if (!IsLoggingConfigurationLoaded(config.Configuration)) { - var standardPath = Path.Combine(contentRootPath, "NLog.config"); - if (File.Exists(standardPath)) + config.Configuration = config.LogFactory.Configuration; + if (!IsLoggingConfigurationLoaded(config.Configuration)) { - config.Configuration = new XmlLoggingConfiguration(standardPath, config.LogFactory); - } - else - { - var lowercasePath = System.IO.Path.Combine(contentRootPath, "nlog.config"); - if (File.Exists(lowercasePath)) - { - config.Configuration = new XmlLoggingConfiguration(lowercasePath, config.LogFactory); - } - else - { - config.Configuration = null; // Perform default loading - } + config.Configuration = LoadXmlLoggingConfigurationFromPath(contentRootPath, config.LogFactory); } } }); } + private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath(string contentRootPath, LogFactory logFactory) + { + var standardPath = System.IO.Path.Combine(contentRootPath, "NLog.config"); + if (System.IO.File.Exists(standardPath)) + { + return new XmlLoggingConfiguration(standardPath, logFactory); + } + else + { + var lowercasePath = System.IO.Path.Combine(contentRootPath, "nlog.config"); + if (System.IO.File.Exists(lowercasePath)) + { + return new XmlLoggingConfiguration(lowercasePath, logFactory); + } + else + { + return null; // Perform default loading + } + } + } + + private static bool IsLoggingConfigurationLoaded(LoggingConfiguration cfg) + { + return cfg?.LoggingRules?.Count > 0 && cfg?.AllTargets?.Count > 0; + } + private static IConfiguration SetupNLogConfigSettings(IServiceProvider serviceProvider, IConfiguration configuration) { ServiceLocator.ServiceProvider = serviceProvider; diff --git a/tests/Shared/LayoutRenderers/AspNetRequestUrlRendererTests.cs b/tests/Shared/LayoutRenderers/AspNetRequestUrlRendererTests.cs index 620199c0..dad73ac0 100644 --- a/tests/Shared/LayoutRenderers/AspNetRequestUrlRendererTests.cs +++ b/tests/Shared/LayoutRenderers/AspNetRequestUrlRendererTests.cs @@ -1,15 +1,13 @@ -using NLog.Web.Enums; +using System; +using NLog.Web.Enums; using NLog.Web.LayoutRenderers; using NSubstitute; using Xunit; -using System; #if ASP_NET_CORE using System.IO; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; -#else -using System; #endif namespace NLog.Web.Tests.LayoutRenderers