Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog: Format loglevel with Whitespaces

Tags:

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?

like image 738
user3114804 Avatar asked Dec 18 '13 10:12

user3114804


1 Answers

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)

like image 183
wageoghe Avatar answered Oct 20 '22 17:10

wageoghe