Skip to content

Commit 305e2da

Browse files
committed
UseNLog allow fallback to only EnvironmentName for NLog config
1 parent c364a94 commit 305e2da

File tree

2 files changed

+77
-57
lines changed

2 files changed

+77
-57
lines changed

src/NLog.Web.AspNetCore/AspNetExtensions.cs

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,27 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
387387
provider.LogFactory.ServiceRepository.RegisterService(typeof(IServiceProvider), serviceProvider);
388388
}
389389

390-
if (configuration != null)
390+
if (configuration is null || !TryLoadConfigurationFromSection(provider, configuration))
391391
{
392-
TryLoadConfigurationFromSection(provider, configuration);
393-
}
392+
string nlogConfigFile = string.Empty;
393+
string contentRootPath = hostEnvironment?.ContentRootPath;
394+
string environmentName = hostEnvironment?.EnvironmentName;
395+
if (!string.IsNullOrWhiteSpace(contentRootPath) || !string.IsNullOrWhiteSpace(environmentName))
396+
{
397+
provider.LogFactory.Setup().LoadConfiguration(cfg =>
398+
{
399+
if (!IsLoggingConfigurationLoaded(cfg.Configuration))
400+
{
401+
nlogConfigFile = ResolveEnvironmentNLogConfigFile(contentRootPath, environmentName);
402+
cfg.Configuration = null;
403+
}
404+
});
405+
}
394406

395-
var contentRootPath = hostEnvironment?.ContentRootPath;
396-
if (!string.IsNullOrWhiteSpace(contentRootPath))
397-
{
398-
TryLoadConfigurationFromContentRootPath(provider.LogFactory, contentRootPath, hostEnvironment.EnvironmentName);
407+
if (!string.IsNullOrEmpty(nlogConfigFile))
408+
{
409+
provider.LogFactory.Setup().LoadConfigurationFromFile(nlogConfigFile, optional: true);
410+
}
399411
}
400412

401413
if (provider.Options.ShutdownOnDispose || !provider.Options.AutoShutdown)
@@ -406,28 +418,32 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
406418
return provider;
407419
}
408420

409-
private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactory, string contentRootPath, string environmentName)
421+
private static string ResolveEnvironmentNLogConfigFile(string basePath, string environmentName)
410422
{
411-
logFactory.Setup().LoadConfiguration(config =>
423+
if (!string.IsNullOrWhiteSpace(basePath))
412424
{
413-
if (IsLoggingConfigurationLoaded(config.Configuration))
414-
return;
415-
416-
if (!string.IsNullOrEmpty(environmentName))
425+
if (!string.IsNullOrWhiteSpace(environmentName))
417426
{
418-
var nlogConfig = LoadXmlLoggingConfigurationFromPath(contentRootPath, $"NLog.{environmentName}.config", config.LogFactory) ??
419-
LoadXmlLoggingConfigurationFromPath(contentRootPath, $"nlog.{environmentName}.config", config.LogFactory) ??
420-
LoadXmlLoggingConfigurationFromPath(contentRootPath, "NLog.config", config.LogFactory) ??
421-
LoadXmlLoggingConfigurationFromPath(contentRootPath, "nlog.config", config.LogFactory);
422-
config.Configuration = nlogConfig;
427+
var nlogConfigEnvFilePath = System.IO.Path.Combine(basePath, $"nlog.{environmentName}.config");
428+
if (System.IO.File.Exists(nlogConfigEnvFilePath))
429+
return System.IO.Path.GetFullPath(nlogConfigEnvFilePath);
430+
nlogConfigEnvFilePath = System.IO.Path.Combine(basePath, $"NLog.{environmentName}.config");
431+
if (System.IO.File.Exists(nlogConfigEnvFilePath))
432+
return System.IO.Path.GetFullPath(nlogConfigEnvFilePath);
423433
}
424-
else
425-
{
426-
var nlogConfig = LoadXmlLoggingConfigurationFromPath(contentRootPath, "NLog.config", config.LogFactory) ??
427-
LoadXmlLoggingConfigurationFromPath(contentRootPath, "nlog.config", config.LogFactory);
428-
config.Configuration = nlogConfig;
429-
}
430-
});
434+
435+
var nlogConfigFilePath = System.IO.Path.Combine(basePath, "nlog.config");
436+
if (System.IO.File.Exists(nlogConfigFilePath))
437+
return System.IO.Path.GetFullPath(nlogConfigFilePath);
438+
nlogConfigFilePath = System.IO.Path.Combine(basePath, "NLog.config");
439+
if (System.IO.File.Exists(nlogConfigFilePath))
440+
return System.IO.Path.GetFullPath(nlogConfigFilePath);
441+
}
442+
443+
if (!string.IsNullOrWhiteSpace(environmentName))
444+
return $"nlog.{environmentName}.config";
445+
446+
return null;
431447
}
432448

433449
private static LoggingConfiguration LoadXmlLoggingConfigurationFromPath(string contentRootPath, string nlogConfigFileName, LogFactory logFactory)
@@ -451,10 +467,10 @@ private static IConfiguration SetupNLogConfigSettings(IServiceProvider servicePr
451467
return configuration;
452468
}
453469

454-
private static void TryLoadConfigurationFromSection(NLogLoggerProvider loggerProvider, IConfiguration configuration)
470+
private static bool TryLoadConfigurationFromSection(NLogLoggerProvider loggerProvider, IConfiguration configuration)
455471
{
456472
if (string.IsNullOrEmpty(loggerProvider.Options.LoggingConfigurationSectionName))
457-
return;
473+
return false;
458474

459475
var nlogConfig = configuration.GetSection(loggerProvider.Options.LoggingConfigurationSectionName);
460476
if (nlogConfig?.GetChildren()?.Any() == true)
@@ -466,10 +482,12 @@ private static void TryLoadConfigurationFromSection(NLogLoggerProvider loggerPro
466482
configBuilder.Configuration = new NLogLoggingConfiguration(nlogConfig, loggerProvider.LogFactory);
467483
}
468484
});
485+
return true;
469486
}
470487
else
471488
{
472489
Common.InternalLogger.Debug("Skip loading NLogLoggingConfiguration from empty config section: {0}", loggerProvider.Options.LoggingConfigurationSectionName);
490+
return false;
473491
}
474492
}
475493
}

src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,44 @@ public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder
4545
// "NLog"-section in appsettings.json has first priority
4646
return setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config)).LoadConfigurationFromSection(config, nlogConfigSection);
4747
}
48-
else
48+
49+
setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config));
50+
51+
var nlogConfigFile = ResolveEnvironmentNLogConfigFile(basePath, environment);
52+
if (!string.IsNullOrEmpty(nlogConfigFile))
4953
{
50-
setupBuilder.SetupExtensions(e => e.RegisterNLogWeb().RegisterConfigSettings(config));
54+
return setupBuilder.LoadConfigurationFromFile(nlogConfigFile, optional: true);
55+
}
5156

52-
if (!string.IsNullOrEmpty(basePath))
53-
{
54-
if (!string.IsNullOrEmpty(environment))
55-
{
56-
setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, $"nlog.{environment}.config"), optional: true);
57-
setupBuilder.LoadConfiguration(config =>
58-
{
59-
if (!IsLoggingConfigurationLoaded(config.Configuration))
60-
{
61-
// Fallback when environment-specific NLog config could not load
62-
var nlogConfigFilePath = Path.Combine(basePath, "nlog.config");
63-
config.Configuration = File.Exists(nlogConfigFilePath) ? new XmlLoggingConfiguration(nlogConfigFilePath, config.LogFactory) : null;
64-
}
65-
});
66-
}
67-
else
68-
{
69-
setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, "nlog.config"), optional: true);
70-
}
71-
}
72-
else if (!string.IsNullOrEmpty(environment))
57+
return setupBuilder.LoadConfigurationFromFile(); // No effect, if config already loaded
58+
}
59+
60+
private static string ResolveEnvironmentNLogConfigFile(string basePath, string environmentName)
61+
{
62+
if (!string.IsNullOrWhiteSpace(basePath))
63+
{
64+
if (!string.IsNullOrWhiteSpace(environmentName))
7365
{
74-
setupBuilder.LoadConfigurationFromFile($"nlog.{environment}.config", optional: true);
66+
var nlogConfigEnvFilePath = Path.Combine(basePath, $"nlog.{environmentName}.config");
67+
if (File.Exists(nlogConfigEnvFilePath))
68+
return Path.GetFullPath(nlogConfigEnvFilePath);
69+
nlogConfigEnvFilePath = Path.Combine(basePath, $"NLog.{environmentName}.config");
70+
if (File.Exists(nlogConfigEnvFilePath))
71+
return Path.GetFullPath(nlogConfigEnvFilePath);
7572
}
7673

77-
return setupBuilder.LoadConfigurationFromFile(); // No effect, if config already loaded
74+
var nlogConfigFilePath = Path.Combine(basePath, "nlog.config");
75+
if (File.Exists(nlogConfigFilePath))
76+
return Path.GetFullPath(nlogConfigFilePath);
77+
nlogConfigFilePath = Path.Combine(basePath, "NLog.config");
78+
if (File.Exists(nlogConfigFilePath))
79+
return Path.GetFullPath(nlogConfigFilePath);
7880
}
81+
82+
if (!string.IsNullOrWhiteSpace(environmentName))
83+
return $"nlog.{environmentName}.config";
84+
85+
return null;
7986
}
8087

8188
private static string ResolveCurrentAppDirectory()
@@ -92,11 +99,6 @@ private static string ResolveCurrentAppDirectory()
9299
return currentBasePath;
93100
}
94101

95-
private static bool IsLoggingConfigurationLoaded(LoggingConfiguration cfg)
96-
{
97-
return cfg?.LoggingRules?.Count > 0 && cfg?.AllTargets?.Count > 0;
98-
}
99-
100102
private static string GetAspNetCoreEnvironment(string variableName)
101103
{
102104
try

0 commit comments

Comments
 (0)