Skip to content

Commit

Permalink
Loading NLog.config from contentRootPath as last fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Aug 31, 2022
1 parent d8cc5ae commit bbbd885
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
34 changes: 21 additions & 13 deletions src/NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using NLog.Config;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -402,29 +401,38 @@ 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))
var standardPath = System.IO.Path.Combine(contentRootPath, "NLog.config");
if (System.IO.File.Exists(standardPath))
{
config.Configuration = new XmlLoggingConfiguration(lowercasePath, config.LogFactory);
config.Configuration = new XmlLoggingConfiguration(standardPath, config.LogFactory);
}
else
{
config.Configuration = null; // Perform default loading
var lowercasePath = System.IO.Path.Combine(contentRootPath, "nlog.config");
if (System.IO.File.Exists(lowercasePath))
{
config.Configuration = new XmlLoggingConfiguration(lowercasePath, config.LogFactory);
}
else
{
config.Configuration = 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;
Expand Down
6 changes: 2 additions & 4 deletions tests/Shared/LayoutRenderers/AspNetRequestUrlRendererTests.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit bbbd885

Please sign in to comment.