.Net Framework 4.8 应用程序在应用服务上运行。
(该项目目前已为 NLog 安装了以下所有 Nuget 软件包)
NLog 4.7.15
NLog.Config 4.5.9
NLog.Extended 4.5.9
NLog.Web 4.6.0
NLog.Schema 4.5.9
假设我们在 web.config 的应用程序设置和连接字符串中有一个值
<connectionStrings>
<add name="ElasticUrl" connectionString="http://localhost:9200"/>
</connectionStrings>
<appSettings>
<add key="testRobot" value="12345" />
</appSettings>
我的问题是可以在 NLog.config 文件中获取这些值
例如,在 Nlog 文件中,我们使用
设置日期 <parameter name="@logged" layout="${date}" />
可以这样说吗
<parameter name="@logged" layout="${ConfigurationManager.AppSettings["testRobot"]}" />
感谢您的回复
更新
感谢@Harshitha 和@Rolf Kristensen 花时间回复。
我能够让它工作(对于appsettings值),但与文档中的建议略有不同,他们使用“item”来检索内容,但我必须使用“name”
https://github.com/NLog/NLog/wiki/AppSetting-Layout-Renderer
<parameter name="@message" layout="${appsetting:name=testRobot:default=default}" />
将在消息中存储值 12345。
但是,这不适用于连接字符串...根据文档(刚刚提供的链接),如果我有
<configuration>
<appSettings>
<add key="MyKey" value="MyApplication" />
</appSettings>
<connectionStrings>
<add name="ElasticUrl" connectionString="http://localhost:9200"/>
</connectionStrings>
</configuration>
我应该可以说:
${appsetting:item=connectionStrings.ElasticUrl}
这不起作用,失败: 未指定“布局渲染器:${appsetting}”上必需的参数“名称”。
也没有:
${appsetting:name=connectionStrings.ElasticUrl}
没有抛出异常,而是将消息保存为空字符串值??
也只是一个观察,但当连接字符串值不在应用程序设置标记内时,我们为什么要使用
$(appsetting
请问有什么帮助吗?
您可以使用 ${appsetting},另请参阅可用布局渲染器列表
${appsetting:name=testRobot:default=default}
请注意,当使用 NLog v4.6.5(或更高版本)时,还可以像这样解析 ConnectionStings 值:
${appsetting:item=connectionStrings.ElasticUrl}
并从
app.config
文件中检索值,如下所示:
<configuration>
<appSettings>
<add key="MyKey" value="MyApplication" />
</appSettings>
<connectionStrings>
<add name="ElasticUrl" connectionString="http://localhost:9200"/>
</connectionStrings>
</configuration>