Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading NLog.config from contentRootPath as last fallback #861

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 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,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;
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