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

UseNLog allow fallback to only EnvironmentName for NLog config #773

Merged
merged 1 commit into from
Nov 21, 2024
Merged
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
66 changes: 39 additions & 27 deletions src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -53,45 +54,56 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
{
NLogLoggerProvider provider = serviceProvider.CreateNLogLoggerProvider(hostConfiguration, options, null);

var contentRootPath = hostEnvironment?.ContentRootPath;
if (!string.IsNullOrWhiteSpace(contentRootPath))
string nlogConfigFile = string.Empty;
string contentRootPath = hostEnvironment?.ContentRootPath;
string environmentName = hostEnvironment?.EnvironmentName;
if (!string.IsNullOrWhiteSpace(contentRootPath) || !string.IsNullOrWhiteSpace(environmentName))
{
TryLoadConfigurationFromContentRootPath(provider.LogFactory, contentRootPath, hostEnvironment?.EnvironmentName);
provider.LogFactory.Setup().LoadConfiguration(cfg =>
{
if (!IsLoggingConfigurationLoaded(cfg.Configuration))
{
nlogConfigFile = ResolveEnvironmentNLogConfigFile(contentRootPath, environmentName);
cfg.Configuration = null;
}
});
}

if (!string.IsNullOrEmpty(nlogConfigFile))
{
provider.LogFactory.Setup().LoadConfigurationFromFile(nlogConfigFile, optional: true);
}

provider.LogFactory.Setup().SetupLogFactory(ext => ext.AddCallSiteHiddenAssembly(typeof(ConfigureExtensions).Assembly));
return provider;
}

private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactory, string contentRootPath, string environmentName)
private static string ResolveEnvironmentNLogConfigFile(string basePath, string environmentName)
{
logFactory.Setup().LoadConfiguration(config =>
if (!string.IsNullOrWhiteSpace(basePath))
{
if (IsLoggingConfigurationLoaded(config.Configuration))
return;

if (!string.IsNullOrEmpty(environmentName))
{
var nlogConfig = LoadXmlLoggingConfigurationFromPath(contentRootPath, $"NLog.{environmentName}.config", config.LogFactory) ??
LoadXmlLoggingConfigurationFromPath(contentRootPath, $"nlog.{environmentName}.config", config.LogFactory) ??
LoadXmlLoggingConfigurationFromPath(contentRootPath, "NLog.config", config.LogFactory) ??
LoadXmlLoggingConfigurationFromPath(contentRootPath, "nlog.config", config.LogFactory);
config.Configuration = nlogConfig;
}
else
if (!string.IsNullOrWhiteSpace(environmentName))
{
var nlogConfig = LoadXmlLoggingConfigurationFromPath(contentRootPath, "NLog.config", config.LogFactory) ??
LoadXmlLoggingConfigurationFromPath(contentRootPath, "nlog.config", config.LogFactory);
config.Configuration = nlogConfig;
var nlogConfigEnvFilePath = Path.Combine(basePath, $"nlog.{environmentName}.config");
if (File.Exists(nlogConfigEnvFilePath))
return Path.GetFullPath(nlogConfigEnvFilePath);
nlogConfigEnvFilePath = Path.Combine(basePath, $"NLog.{environmentName}.config");
if (File.Exists(nlogConfigEnvFilePath))
return Path.GetFullPath(nlogConfigEnvFilePath);
}
});
}

private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath(string contentRootPath, string nlogConfigFileName, LogFactory logFactory)
{
var standardPath = System.IO.Path.Combine(contentRootPath, nlogConfigFileName);
return System.IO.File.Exists(standardPath) ?
new XmlLoggingConfiguration(standardPath, logFactory) : null;
var nlogConfigFilePath = Path.Combine(basePath, "nlog.config");
if (File.Exists(nlogConfigFilePath))
return Path.GetFullPath(nlogConfigFilePath);
nlogConfigFilePath = Path.Combine(basePath, "NLog.config");
if (File.Exists(nlogConfigFilePath))
return Path.GetFullPath(nlogConfigFilePath);
}

if (!string.IsNullOrWhiteSpace(environmentName))
return $"nlog.{environmentName}.config";

return null;
}

private static bool IsLoggingConfigurationLoaded(LoggingConfiguration cfg)
Expand Down