【问题标题】:JsonLayout with non formatted message带有非格式化消息的 JsonLayout
【发布时间】:2021-08-02 11:32:03
【问题描述】:

我对 JsonLayout 有点问题

NLog 版本:4.7.10 平台:netcoreapp 3.1

当前的 Nlog 配置

<target name="jsonFileMw" xsi:type="File" fileName="logs\mw.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" }

我怎样才能做到这一点?

【问题讨论】:

标签: nlog


【解决方案1】:

你可以这样做:

<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

【讨论】:

    猜你喜欢
    • 2015-07-28
    • 2012-08-13
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多