【问题标题】:NLog adding backSlash with Json LayoutNLog 使用 Json 布局添加反斜杠
【发布时间】:2020-08-13 06:15:14
【问题描述】:

我找不到如何告诉 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\"}"

知道怎么解决吗?

【问题讨论】:

    标签: c# json nlog


    【解决方案1】:

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

    您可以使用encode="false" 禁用此功能,如下所示:

     <attribute name="Payload" encode="false" escapeForwardSlash="false" layout="${aspnet-request-posted-body:when=level==LogLevel.Trace or level==LogLevel.Error or level==LogLevel.Fatal}"/>
    

    See docs

    【讨论】:

    • 如果属性被添加为消息属性呢?
    • 这是什么意思?请编辑您的问题或提出新问题,因为实际上不允许提出多个问题
    【解决方案2】:

    如果你能确保 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

    【讨论】:

      猜你喜欢
      • 2016-06-14
      • 2014-10-14
      • 2015-10-23
      • 2017-06-08
      • 2021-07-30
      • 1970-01-01
      • 2019-07-04
      • 2016-07-09
      • 1970-01-01
      相关资源
      最近更新 更多