|
1 | 1 | using System;
|
| 2 | +using System.IO; |
2 | 3 | using System.Reflection;
|
3 | 4 | using Microsoft.Extensions.Configuration;
|
4 | 5 | using Microsoft.Extensions.DependencyInjection;
|
@@ -53,45 +54,56 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv
|
53 | 54 | {
|
54 | 55 | NLogLoggerProvider provider = serviceProvider.CreateNLogLoggerProvider(hostConfiguration, options, null);
|
55 | 56 |
|
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)) |
58 | 61 | {
|
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 | + }); |
60 | 70 | }
|
61 | 71 |
|
| 72 | + if (!string.IsNullOrEmpty(nlogConfigFile)) |
| 73 | + { |
| 74 | + provider.LogFactory.Setup().LoadConfigurationFromFile(nlogConfigFile, optional: true); |
| 75 | + } |
| 76 | + |
62 | 77 | provider.LogFactory.Setup().SetupLogFactory(ext => ext.AddCallSiteHiddenAssembly(typeof(ConfigureExtensions).Assembly));
|
63 | 78 | return provider;
|
64 | 79 | }
|
65 | 80 |
|
66 |
| - private static void TryLoadConfigurationFromContentRootPath(LogFactory logFactory, string contentRootPath, string environmentName) |
| 81 | + private static string ResolveEnvironmentNLogConfigFile(string basePath, string environmentName) |
67 | 82 | {
|
68 |
| - logFactory.Setup().LoadConfiguration(config => |
| 83 | + if (!string.IsNullOrWhiteSpace(basePath)) |
69 | 84 | {
|
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)) |
82 | 86 | {
|
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); |
86 | 93 | }
|
87 |
| - }); |
88 |
| - } |
89 | 94 |
|
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; |
95 | 107 | }
|
96 | 108 |
|
97 | 109 | private static bool IsLoggingConfigurationLoaded(LoggingConfiguration cfg)
|
|
0 commit comments