3

I'm new to NLog and I confused with layout and layout-renderers.

I saw following 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 (something like the first)

I understand first (format of log message), but what is second and third I don't understand.

Community
  • 1
  • 1
Denis535
  • 3,407
  • 4
  • 25
  • 36

1 Answers1

5
  • Layouts are defining the layout of the rendered output, examples are a CSV, JSON, XML (NLog 4.6+) or plain layout (the default). There are currently 5 layouts in NLog (including the default): https://nlog-project.org/config/?tab=layouts
  • Layout renderers are rendering values, e.g. a message, an exception, the process id etc. Also called "template markers". The layout renders looking as ${something}. There are around 100 layout renderers in NLog, but there are also some 3rd party layout renderers. See https://nlog-project.org/config/?tab=layout-renderers

You can see a layout as a method to combine layout renderers. The default layout is a bit hidden, but other layouts should make it more clear. See the examples below.

Some examples:

Default layout

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

This is the default layout with 4 layout renderers (date, level, message, exception)

JSON layout

A file target with a JsonLayout with the same 4 layout renderers:

<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>

This will create file with e.g. { "time": "2016-10-30 13:30:55.0000", "level": "INFO", "message": "this is message", "exception": "test" }

The same for CSV, but then to create CSV files (or CSV to the database etc).

Exception layout renderer: ${exception}

(See also https://github.com/NLog/NLog/wiki/Exception-Layout-Renderer)

This is for rendering the exception, as exceptions are captured separate from the message in NLog. See also How to proper log exceptions

Julian
  • 33,915
  • 22
  • 119
  • 174
  • Thank you! I was confused because your page https://nlog-project.org/config/?tab=layouts hasn't default layout description. And other layouts have a different format than default layout. – Denis535 Feb 17 '19 at 16:40
  • 1
    @wishmaster35 thanks for you input. I have added the default layout to https://nlog-project.org/config/?tab=layouts – Julian Feb 17 '19 at 22:55
  • I have not found the information, can I use different layouts for different levels? Also can I use function to produce log message? Because complex layout string may be too complicated. – Denis535 Feb 17 '19 at 23:32
  • Good question, I think those should be separate questions as I need more than a comment to answer those. – Julian Feb 18 '19 at 10:18
  • Okey. I open two questions: https://stackoverflow.com/questions/54748338/nlog-how-to-use-function-plain-code-instead-of-layout-string https://stackoverflow.com/questions/54747376/nlog-how-to-use-different-layout-for-different-levels – Denis535 Feb 18 '19 at 15:11