NLog使用Json Layout添加backSlash

问题描述

我找不到一种方法来告诉NLog正确打印我的JSON而没有反斜杠。.

这里是我的Nlog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwConfigExceptions="true">
  <targets async="true">
    <target name="jsonFile" xsi:type="File" fileName="//share/Logs/Ws/${shortdate}.json" >
      <layout xsi:type="JsonLayout">
        <attribute name="Timestamp" layout="${longdate}"/>
        <attribute name="Level" layout="${level:upperCase=true}"/>
        <attribute name="SourceContext" layout="${logger}"/>
        <attribute name="MachineName" layout="${machinename}"/>
        <attribute name="ProcessId" layout="${processid}"/>
        <attribute name="ThreadId" layout="${threadid}"/>
        <attribute name="Assembly" layout="${Assembly-Name}"/>
        <attribute name="AssemblyVersion" layout="${Assembly-Version}"/>
        <attribute name="Message" layout="${message}"/>
        <attribute name="Exception" layout="${exception:format=@}" encode="false"/>
        <attribute name="Properties" encode="false">
          <layout type='JsonLayout' includeAllProperties="true"  maxRecursionLimit="10">
            <attribute name="ActivityId" layout="${activityid}"/>
            <attribute name="Method" layout="${aspnet-request-method}"/>
            <attribute name="ClientIp" layout="${aspnet-request-ip}"/>
            <attribute name="Url" layout="${aspnet-request-url}"/>
            <attribute name="Querystring" layout="${aspnet-request-querystring:when=level==LogLevel.Trace or level==LogLevel.Error or level==LogLevel.Fatal}"/>
            <attribute name="Headers" layout="${aspnet-request-headers:when=level==LogLevel.Trace or level==LogLevel.Warn or level==LogLevel.Error or level==LogLevel.Fatal}"/>
            <attribute name="Payload" escapeForwardSlash="false" layout="${aspnet-request-posted-body:when=level==LogLevel.Trace or level==LogLevel.Error or level==LogLevel.Fatal}"/>
          </layout>
        </attribute>
      </layout>
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeto="jsonFile" />
  </rules>
</nlog>

但是当它打印到文件时,我得到了这个例子:

"Payload": "{\"name\":\"value\",\"surname\":\"value\"}"

有什么想法要解决吗?

解决方法

它逃脱了有效载荷,因为它不知道它已经是JSON。

您可以使用LocalDate禁用此功能,如下所示:

encode="false"

See docs

,

是否可以确保HttpRequest ContentType为application/json的随机提示:

<layout xsi:type="JsonLayout">
   <attribute name="Properties" encode="false">
        <layout type='JsonLayout' includeAllProperties="true"  maxRecursionLimit="10">
           <attribute name="Payload" encode="false" layout="${when:when='${aspnet-request-contenttype}'=='application/json':inner=${aspnet-request-posted-body}}" />
           <attribute name="Payload" layout="${when:when='${aspnet-request-contenttype}'!='application/json':inner=${aspnet-request-posted-body}}" />
        </layout>
   </attribute>
</layout>

另请参阅:https://github.com/NLog/NLog/wiki/AspNet-Request-ContentType-layout-renderer