8000 UseNLog fallback to only EnvironmentName when loading NLog config (#773) · NLog/NLog.Extensions.Logging@c9f4aa9 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9f4aa9

Browse files
authored
UseNLog fallback to only EnvironmentName when loading NLog config (#773)
1 parent 95932f4 commit c9f4aa9

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

src/NLog.Extensions.Hosting/Extensions/ConfigureExtensions.cs

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Reflection;
34
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.DependencyInjection;
@@ -53,45 +54,56 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
5354
{
5455
NLogLoggerProvider provider = serviceProvider.CreateNLogLoggerProvider(hostConfiguration, options, null);
5556

56-
var contentRootPath = hostEnvironment?.ContentRootPath;
57-
if (!string.IsNullOrWhiteSpace(contentRootPath))
57+
string nlogConfigFile = string.Empty;
58+
string contentRootPath = hostEnvironment?.ContentRootPath;
59+
string environmentName = hostEnvironment?.EnvironmentName;< 8000 /div>
60+
if (!string.IsNullOrWhiteSpace(contentRootPath) || !string.IsNullOrWhiteSpace(environmentName))
5861
{
59-
TryLoadConfigurationFromContentRootPath(provider.LogFactory, contentRootPath, hostEnvironment?.EnvironmentName);
62+
provider.LogFactory.Setup().LoadConfiguration(cfg =>
63+
{
64+
if (!IsLoggingConfigurationLoaded(cfg.Configuration))
65+
{
66+
nlogConfigFile = ResolveEnvironmentNLogConfigFile(contentRootPath, environmentName);
67+
cfg.Configuration = null;
68+
}
69+
});
6070
}
6171

72+
if (!string.IsNullOrEmpty(nlogConfigFile))
73+
{
74+
provider.LogFactory.Setup().LoadConfigurationFromFile(nlogConfigFile, optional: true);
75+
}
76+
6277
provider.LogFactory.Setup().SetupLogFactory(ext => ext.AddCallSiteHiddenAssembly(typeof(ConfigureExtensions).Assembly));
6378
return provider;
6479
}
6580

66-
private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactory, string contentRootPath, string environmentName)
81+
private static string ResolveEnvironmentNLogConfigFile(string basePath, string environmentName)
6782
{
68-
logFactory.Setup().LoadConfiguration(config =>
83+
if (!string.IsNullOrWhiteSpace(basePath))
6984
{
70-
if (IsLoggingConfigurationLoaded(config.Configuration))
71-
return;
72-
73-
if (!string.IsNullOrEmpty(environmentName))
74-
{
75-
var nlogConfig = LoadXmlLoggingConfigurationFromPath(contentRootPath, $"NLog.{environmentName}.config", config.LogFactory) ??
76-
LoadXmlLoggingConfigurationFromPath(contentRootPath, $"nlog.{environmentName}.config", config.LogFactory) ??
77-
LoadXmlLoggingConfigurationFromPath(contentRootPath, "NLog.config", config.LogFactory) ??
78-
LoadXmlLoggingConfigurationFromPath(contentRootPath, "nlog.config", config.LogFactory);
79-
config.Configuration = nlogConfig;
80-
}
81-
else
85+
if (!string.IsNullOrWhiteSpace(environmentName))
8286
{
83-
var nlogConfig = LoadXmlLoggingConfigurationFromPath(contentRootPath, "NLog.config", config.LogFactory) ??
84-
LoadXmlLoggingConfigurationFromPath(contentRootPath, "nlog.config", config.LogFactory);
85-
config.Configuration = nlogConfig;
87+
var nlogConfigEnvFilePath = Path.Combine(basePath, $"nlog.{environmentName}.config");
88+
if (File.Exists(nlogConfigEnvFilePath))
89+
return Path.GetFullPath(nlogConfigEnvFilePath);
90+
nlogConfigEnvFilePath = Path.Combine(basePath, $"NLog.{environmentName}.config");
91+
if (File.Exists(nlogConfigEnvFilePath))
92+
return Path.GetFullPath(nlogConfigEnvFilePath);
8693
}
87-
});
88-
}
8994

90-
private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath(string contentRootPath, string nlogConfigFileName, LogFactory logFactory)
91-
{
92-
var standardPath = System.IO.Path.Combine(contentRootPath, nlogConfigFileName);
93-
return System.IO.File.Exists(standardPath) ?
94-
new XmlLoggingConfiguration(standardPath, logFactory) : null;
95+
var nlogConfigFilePath = Path.Combine(basePath, "nlog.config");
96+
if (File.Exists(nlogConfigFilePath))
97+
return Path.GetFullPath(nlogConfigFilePath);
98+
nlogConfigFilePath = Path.Combine(basePath, "NLog.config");
99+
if (File.Exists(nlogConfigFilePath))
100+
return Path.GetFullPath(nlogConfigFilePath);
101+
}
102+
103+
if (!string.IsNullOrWhiteSpace(environmentName))
104+
return $"nlog.{environmentName}.config";
105+
106+
return null;
95107
}
96108

97109
private static bool IsLoggingConfigurationLoaded(LoggingConfiguration cfg)

0 commit comments

Comments
 (0)
0