NLog:使用Whitespaces格式化日志级别 [英] NLog: Format loglevel with Whitespaces

查看:288
本文介绍了NLog:使用Whitespaces格式化日志级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NLog进行日志记录。
目前我的布局字符串是:

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

结果如下:

  18.12.2013 11:23:14,834 | INFO |这是一个信息
18.12.2013 11:23:14,835 | TRACE |这是一个跟踪

现在我想要的是格式化$ level来填充空白所以它看起来像一个5字符的表。



我想要:

  18.12.2013 11:23:14,834 | INFO |这是一个信息
18.12.2013 11:23:14,835 | TRACE |这是一个跟踪

我没有发现任何可悲的事情...任何人都可以帮忙吗?



非常感谢!

解决方案

尝试使用PaddingLayoutRendererWrapper。我不确定哪里有一个好的配置示例,但是NLog的源代码库中的源代码位于此处,所以也许可以反向设计正确的配置:

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



我认为你可以这样做:

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

将在左侧填充5个空格的所有日志级别值,然后修剪为5的绝对长度。

padding = 5 表示将5个填充字符(默认为')添加到左侧(负数表示填充右侧)

fixedlength = true 是一个布尔值即表示填充后的结果应该被修剪到填充(即填充)的最大长度。 5在我的例子)


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?

Thanks a lot!

解决方案

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)

这篇关于NLog:使用Whitespaces格式化日志级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆