我对JsonLayout没有什么问题
NLog版本号:4.7.10平台:netcoreapp 3.1
当前Nlog配置
<target name="jsonFileMw" xsi:type="File" fileName="logsmw.log"
archiveAboveSize="10240"
maxArchiveDays="5"
archiveNumbering="DateAndSequence"
archiveEvery="Day"
enableArchiveFileCompression="true">
<layout xsi:type="JsonLayout" includeAllProperties="true">
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level:upperCase=true}"/>
<attribute name="message" layout="${message}" />
</layout>
</target>
我的日志代码_logger.LogInformation("request received. {RequestUrl} {RequestBody}", "some url", "some body");
这个日志代码产生如下日志行:
{ "time": "2021-08-02 15:07:30.8198", "level": "INFO", "message": "request received. some url some body", "RequestUrl": "some url", "RequestBody": "some body" }
正如您所看到的,这也为消息添加了日志属性,这意味着记录两次相同的信息。因此,日志文件的大小增加。我只是想保持信息简单。期望输出如下:
{ "time": "2021-08-02 15:07:30.8198", "level": "INFO", "message": "request received. {RequestUrl} {RequestBody}", "RequestUrl": "some url", "RequestBody": "some body" }
我怎样才能做到这一点?
你可以这样做:
<attribute name="messagetemplate" layout="${message:raw=true}" />
参见:https://github.com/NLog/NLog/wiki/Message-Layout-Renderer
参见:https://github.com/NLog/NLog/wiki/How-to-use-structured-logging#output-captured-properties