我找不到一种方法来告诉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\"}"你知道怎么修复它吗?
发布于 2020-08-13 16:54:03
它逃脱了有效负载,因为它不知道它已经是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}"/>发布于 2020-08-14 03:08:29
随机的想法,如果你能确保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
https://stackoverflow.com/questions/63389172
复制相似问题