【发布时间】:2017-10-18 07:02:42
【问题描述】:
我试图弄清楚如何将LogEventInfo 对象中的所有属性记录到 JSON 格式的字符串中。根据issue on github,我尝试做这样的事情:
<target xsi:type="ColoredConsole" name="coloredConsole">
<layout xsi:type="JsonLayout">
<attribute name="timestamp" layout="${longdate}"/>
<attribute name="level" layout="${level:uppercase=true}"/>
<attribute name="exception" layout="${onexception:${exception:format=tostring}}" />
<attribute name="properties" encode="false">
<layout type="JsonLayout">
<attribute name="properties" layout="${all-event-properties}" />
</layout>
</attribute>
</layout>
</target>
...但不幸的是,我的属性包含复杂对象(我有两个属性,名称分别为“properties”和“tags”,其中“properties”是IDictionary<string, object>,“tags”是IList<string> LogEventInfo.Properties 属性)根本不序列化。我最终得到了类似这样的东西:
{ "timestamp": "2017-05-18 08:41:28.7730", "level": "INFO", "properties": { "properties": "properties=System.Collections.Generic.Dictionary`2[System.String,System.Object], tags=System.Collections.Generic.List`1[System.String]" } }
我期待(并希望)有一个序列化的 JSON 字典,它可以为我提供日志消息的上下文,但显然这不是我得到的。
如何正确序列化 LogEventInfo 对象中的属性?
【问题讨论】:
标签: logging serialization nlog