NLog:什么是布局和布局渲染器?
NLog: What is layout and layout-renderers?
我是 NLog 的新手,我对布局和布局渲染器感到困惑。
我看到了以下内容 code\pages:
https://github.com/NLog/NLog/wiki/Configuration-API
Layout = @"${date:format=HH\:mm\:ss} ${level} ${message} ${exception}"
-
https://github.com/NLog/NLog/wiki/Exception-Layout-Renderer(类似于第一个)
第一个我明白了(日志消息的格式),但是第二个和第三个我不明白。
- 布局定义渲染输出的布局,例如 CSV、JSON、XML(NLog 4.6+)或普通布局(默认)。 NLog目前有5种布局(包括默认的):https://nlog-project.org/config/?tab=layouts
- 布局渲染器是渲染值,例如消息、异常、进程 ID 等。也称为 "template markers"。布局呈现为
${something}
。 NLog 中大约有 100 个布局渲染器,但也有一些第 3 方布局渲染器。参见 https://nlog-project.org/config/?tab=layout-renderers
您可以将布局视为组合布局渲染器的一种方法。默认布局有点隐藏,但其他布局应该更清楚。请参阅下面的示例。
一些示例:
默认布局
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
我是 NLog 的新手,我对布局和布局渲染器感到困惑。
我看到了以下内容 code\pages:
https://github.com/NLog/NLog/wiki/Configuration-API
Layout = @"${date:format=HH\:mm\:ss} ${level} ${message} ${exception}"
https://github.com/NLog/NLog/wiki/Exception-Layout-Renderer(类似于第一个)
第一个我明白了(日志消息的格式),但是第二个和第三个我不明白。
- 布局定义渲染输出的布局,例如 CSV、JSON、XML(NLog 4.6+)或普通布局(默认)。 NLog目前有5种布局(包括默认的):https://nlog-project.org/config/?tab=layouts
- 布局渲染器是渲染值,例如消息、异常、进程 ID 等。也称为 "template markers"。布局呈现为
${something}
。 NLog 中大约有 100 个布局渲染器,但也有一些第 3 方布局渲染器。参见 https://nlog-project.org/config/?tab=layout-renderers
您可以将布局视为组合布局渲染器的一种方法。默认布局有点隐藏,但其他布局应该更清楚。请参阅下面的示例。
一些示例:
默认布局
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