【发布时间】:2017-06-07 08:21:13
【问题描述】:
我有一个带有 NLog 配置的 .Net 项目,它允许我生成 JSON 格式的日志文件。它适用于简单的短信。现在,我需要记录一些已经序列化为 JSON 的任意对象。当我将这些 JSON 字符串记录为日志消息时,NLog 会将文本放在引号中,然后转义内部 JSON 语法。这使得这样的输出不可用。到目前为止,我还没有找到一个 NLog 功能或布局设置,它可以简单地将我的 JSON 字符串转储为文字,没有引号和转义字符。我错过了什么吗?
示例。目前我的日志条目看起来像:]
{ "dateTime": "2017-06-07 11:50:55.7324", "level": "DEBUG", "message": "\"description\": \"blah-blah\", \"Request\": { \"Request URL\":\/somepagepage\/}, \"Content\": { \"Form\": { ... } , \"Body\": \"Blah\" } ", "utcDateTime": "2017-06-05 06:10:34.1411" }
相反,我需要让它们看起来像:
{ "dateTime": "2017-06-07 11:50:55.7324", "level": "DEBUG", "message":
{ "description": "blah-blah", "Request": { "Request URL":/somepagepage/, "Content": { "Form": {...}, "Body": "Blah" } }, "utcDateTime": "2017-06-05 06:10:34.1411" }
来自 NLog.config 的相关部分:
<layout xsi:type="JsonLayout">
<attribute name="dateTime" layout="${longdate}" />
<attribute name="level" layout="${level:upperCase=true}"/>
<attribute name="message" layout="${message}" />
<attribute name="utcDateTime" layout="${longdate:universalTime=true}" />
</layout>
最终,我希望看到一个带有 JSON 嵌套在“消息”中的日志条目,而不是它的引用版本。
【问题讨论】:
-
你的配置是什么?
-
如果将参数 encode="false" 添加到消息元素会发生什么? (属性名称=“消息”编码=“假”...)
-
正是我想要的。谢谢@RolfKristensen - 这成功了!虽然现在纯文本消息没有引号,但我可以编写一个自定义布局来处理这个问题。你应该得到绿色的勾号。