NLog: Format loglevel with Whitespaces

Viewed 8221

I am using NLog for logging. Currently my Layout-String is:

"${date:format=dd.MM.yyyy HH\\:mm\\:ss,fff} | ${level:uppercase=true} | ${message}"

This results in the following Logs:

18.12.2013 11:23:14,834 | INFO | this is an info
18.12.2013 11:23:14,835 | TRACE | this is a trace

What I want now is to format the "$level" to fill up with Whitespaces so that it looks like a table with 5 Characters.

I would like to have:

18.12.2013 11:23:14,834 | INFO  | this is an info
18.12.2013 11:23:14,835 | TRACE | this is a trace

I didn't find anything sadly... Can anyone help?

2 Answers

As of 2022, use the following:

${level:uppercase=true:padding=-5}

Example:

2022-04-15 03:04:58.0764 | INFO  | Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler | AuthenticationScheme: Cookies was challenged.

If you want to align right, use 5 instead of -5.

Reference: https://github.com/NLog/NLog/wiki/Pad-Layout-Renderer

Related