30

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
user3114804
  • 303
  • 3
  • 4

2 Answers2

30

Try using the PaddingLayoutRendererWrapper. I'm not sure where a good config example is, but the source in NLog's source repository is located here, so maybe you can reverse engineer the correct configuration:

https://github.com/NLog/NLog/blob/master/src/NLog/LayoutRenderers/Wrappers/PaddingLayoutRendererWrapper.cs

I think you would do something like this:

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

Hopefully, that example will pad all log level values with 5 spaces on the left and then trim to an absolute length of 5.

padding=5 means to add 5 padcharacters (default is ' ') to left (negative means pad on right)

fixedlength=true is a boolean that indicates that the padded result should be trimmed to a maximum length of "padding" (i.e. 5 in my example)

wageoghe
  • 27,390
  • 13
  • 88
  • 116
  • 26
    Thank you! Padding was the word I'm looking for :-) With your hints I found it out - it's even more simple: ${level:uppercase=true:padding=-5} does it :-) – user3114804 Dec 19 '13 at 07:30
  • 4
    As for 2017, the LayourRenderer name is changed from ${padding} to ${pad}. Besides that, everything works – Marcin Zablocki Mar 06 '17 at 10:27
  • 4
    In case anyone else is searching for an example I wanted to add this example. Note: fixedlength causes an error. ${pad:padding=-25:inner=${date:format=MM/dd/yy HH\:mm\:ss fff}} ${pad:padding=-8:inner=${level:uppercase=true}} ${pad:padding=-25:inner=${logger}} - ${message} – dgxhubbard Mar 22 '18 at 18:48
  • 4
    @dgxhubbard The separator is ':', so this will work ${pad:padding=25:fixedlength=true:inner=${logger}} – Jeroen Jun 11 '18 at 09:57
  • 1
    @Jeroen Oh, thanks for pointing out, the comma there `padding=5,fixedlength=true`, impossible to see it, finally replaced it with `: ` and now works. – mkb Aug 09 '20 at 21:15
  • This should not be the accepted answer since it isn't correct (anymore?). The [answer by stil](https://stackoverflow.com/a/71879068/8521) is much cleaner and actually works using latest nlog. – Isak Savo Sep 16 '22 at 09:02
6

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

stil
  • 5,306
  • 3
  • 38
  • 44