在NLog中没有root参数的webservice目标中的JsonLayout

时间:2018-02-08 20:06:17

标签: c# .net nlog

我想在Web服务目标中使用JsonLayout,以便我可以使用includeAllProperties="true"

<target xsi:type="WebService"
            name="ws"
            url="http://localhost:59802/api/hello/indexer"
            protocol="JsonPost"
            encoding="UTF-8">
<parameter name="root">
  <layout xsi:type="JsonLayout" includeAllProperties="true">
     <attribute name="Level" layout="${level}" />
     <attribute name="Timestamp" layout="${longdate}" />
     <attribute name="Message" layout="${message}" />
  </layout>
</parameter>
</target>

这会创建以下输出

{ "root" : { "level" : "info", "message" : "xxx", "event1" : "aaa" } }

实际上,我想拥有以下内容

{ "level" : "info", "message" : "xxx", "event1" : "aaa" }

Nlog是否支持直接使用JsonLayout而不是包裹<parameter />?或者有任何方法来破解并实现此输出? file and console支持它,但Web服务目标似乎也不支持。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

NLog 4.5允许您使用不带名称的参数。像这样:

<target xsi:type="WebService"
            name="ws"
            url="http://localhost:59802/api/hello/indexer"
            protocol="JsonPost"
            encoding="UTF-8">
<parameter name="">
  <layout xsi:type="JsonLayout" includeAllProperties="true">
     <attribute name="Level" layout="${level}" />
     <attribute name="Timestamp" layout="${longdate}" />
     <attribute name="Message" layout="${message}" />
  </layout>
</parameter>
</target>