NLog:什么是布局和布局渲染器?

NLog: What is layout and layout-renderers?

我是 NLog 的新手,我对布局和布局渲染器感到困惑。

我看到了以下内容 code\pages:

  1. https://github.com/NLog/NLog/wiki/Configuration-API

    Layout = @"${date:format=HH\:mm\:ss} ${level} ${message} ${exception}"

  2. https://github.com/NLog/NLog/wiki/CsvLayout (xml)

  3. https://github.com/NLog/NLog/wiki/Exception-Layout-Renderer(类似于第一个)

第一个我明白了(日志消息的格式),但是第二个和第三个我不明白。

您可以将布局视为组合布局渲染器的一种方法。默认布局有点隐藏,但其他布局应该更清楚。请参阅下面的示例。

一些示例:

默认布局

Layout = @"${date:format=HH\:mm\:ss} ${level} ${message} ${exception}"

这是具有 4 个布局渲染器(日期、级别、消息、异常)的默认布局

JSON布局

具有 JsonLayout 和相同 4 个布局渲染器的文件目标:

<target name='jsonFile' type='File' fileName='log.json'>
  <layout type='JsonLayout'>
    <attribute name='time' layout='${longdate}' />
    <attribute name='level' layout='${level:upperCase=true}'/>
    <attribute name='message' layout='${message}' />
    <attribute name='exception' layout='${exception}' />
  </layout>
</target>

这将创建文件,例如{ "time": "2016-10-30 13:30:55.0000", "level": "INFO", "message": "this is message", "exception": "test" }

CSV 相同,但随后创建 CSV files(或 CSV 到数据库等)。

异常布局渲染器:${exception}

(另见 https://github.com/NLog/NLog/wiki/Exception-Layout-Renderer

这是为了呈现异常,因为异常是与 NLog 中的消息分开捕获的。另见 How to proper log exceptions