如何使用设置变量为 NLog 配置 Azure Table 连接字符串?
How can you configure an Azure Table Connection String for NLog using Settings variables?
我有一个问题,我需要使用 Nlog 将日志写入 Azure table,但连接字符串可能会根据环境(即 Dev/UAT 等)而改变,所以我需要从另一个配置文件中获取它。我的 Nlog 'targets' 部分目前如下所示:
<targets>
<target xsi:type="File" name="allfile" fileName="${var:configDir}\nlog-all.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="AzureTableStorage"
connectionString="${var:myNLogConnectionString}"
name="NLogAzureTable"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}"
tableName="MyTestLogs"
logTimeStampFormat="O" />
</targets>
我的 Api 看起来如下:
ILoggerFactory logger = new LoggerFactory().AddNLog();
var nlogConfigSection = config.Settings.Sections["MyService_NlogSettings"];
LogManager.LoadConfiguration("nlog.config");
LogManager.Configuration = new XmlLoggingConfiguration("nlog.config");
LogManager.Configuration.Variables["configDir"] = nlogConfigSection.Parameters["FileLocation"].Value;
LogManager.Configuration.Variables["myNLogConnectionString"] = nlogConfigSection.Parameters["environmentNLogConnectionString"].Value;
我可以从调试中看到 config.settings 值都按要求被检索,甚至配置中的变量都被适当地填充。我发现如果我在本地写入 'allfile' 文本文件,它会设法检索并填充 'configDir',因为那是文本文件出现的地方!
但是,如果我切换到使用 Azure,我可以看到如前所述,该变量正在配置中设置,但是当我查看 Nlog 内部日志文件时,我可以看到它认为连接字符串为空。
有什么明显的地方我做错了吗?!?我见过类似问题的解决方案,但它们似乎总是涉及做我已经做过的事情,但我一点也不高兴!
尝试这样做:
ILoggerFactory logger = new LoggerFactory().AddNLog();
var nlogConfigSection = config.Settings.Sections["MyService_NlogSettings"];
// Configure global settings before loading NLog.config
NLog.GlobalDiagnosticsContext.Set("configDir", nlogConfigSection.Parameters["FileLocation"].Value);
NLog.GlobalDiagnosticsContext.Set("myNLogConnectionString", nlogConfigSection.Parameters["environmentNLogConnectionString"].Value);
NLog.LogManager.Configuration = new XmlLoggingConfiguration("nlog.config");
使用以下 NLog.config,将 ${var
替换为 ${gdc
:
<targets>
<target xsi:type="File" name="allfile" fileName="${var:configDir}\nlog-all.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="AzureTableStorage"
connectionString="${gdc:myNLogConnectionString}"
name="NLogAzureTable"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}"
tableName="MyTestLogs"
logTimeStampFormat="O" />
</targets>
我有一个问题,我需要使用 Nlog 将日志写入 Azure table,但连接字符串可能会根据环境(即 Dev/UAT 等)而改变,所以我需要从另一个配置文件中获取它。我的 Nlog 'targets' 部分目前如下所示:
<targets>
<target xsi:type="File" name="allfile" fileName="${var:configDir}\nlog-all.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="AzureTableStorage"
connectionString="${var:myNLogConnectionString}"
name="NLogAzureTable"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}"
tableName="MyTestLogs"
logTimeStampFormat="O" />
</targets>
我的 Api 看起来如下:
ILoggerFactory logger = new LoggerFactory().AddNLog();
var nlogConfigSection = config.Settings.Sections["MyService_NlogSettings"];
LogManager.LoadConfiguration("nlog.config");
LogManager.Configuration = new XmlLoggingConfiguration("nlog.config");
LogManager.Configuration.Variables["configDir"] = nlogConfigSection.Parameters["FileLocation"].Value;
LogManager.Configuration.Variables["myNLogConnectionString"] = nlogConfigSection.Parameters["environmentNLogConnectionString"].Value;
我可以从调试中看到 config.settings 值都按要求被检索,甚至配置中的变量都被适当地填充。我发现如果我在本地写入 'allfile' 文本文件,它会设法检索并填充 'configDir',因为那是文本文件出现的地方! 但是,如果我切换到使用 Azure,我可以看到如前所述,该变量正在配置中设置,但是当我查看 Nlog 内部日志文件时,我可以看到它认为连接字符串为空。
有什么明显的地方我做错了吗?!?我见过类似问题的解决方案,但它们似乎总是涉及做我已经做过的事情,但我一点也不高兴!
尝试这样做:
ILoggerFactory logger = new LoggerFactory().AddNLog();
var nlogConfigSection = config.Settings.Sections["MyService_NlogSettings"];
// Configure global settings before loading NLog.config
NLog.GlobalDiagnosticsContext.Set("configDir", nlogConfigSection.Parameters["FileLocation"].Value);
NLog.GlobalDiagnosticsContext.Set("myNLogConnectionString", nlogConfigSection.Parameters["environmentNLogConnectionString"].Value);
NLog.LogManager.Configuration = new XmlLoggingConfiguration("nlog.config");
使用以下 NLog.config,将 ${var
替换为 ${gdc
:
<targets>
<target xsi:type="File" name="allfile" fileName="${var:configDir}\nlog-all.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="AzureTableStorage"
connectionString="${gdc:myNLogConnectionString}"
name="NLogAzureTable"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}"
tableName="MyTestLogs"
logTimeStampFormat="O" />
</targets>