Serilog ExpressionTemplate重命名日志级别并应用格式

发布于 2025-01-27 03:40:23 字数 397 浏览 2 评论 0原文

我正在尝试使用serilog的expressionTemplate并以以下方式写入日志级别:

{ "level": "INF", ... }

我知道如何使@l qualt to last- 级别:@l <:@l << /代码> 而且我知道如何将级别格式化为三个上案例字母 - @l:u3,但我无法将两者结合起来,但

我尝试了以下几个方面的尝试(尝试格式化模板时会失败例外):

level: @l:u3
{'level': @l:u3}

I'm trying to use serilog's ExpressionTemplate and write the log level in the following way:

{ "level": "INF", ... }

I know how to alias @l to level - level: @l
and I know how to format the level to three upper-case letters - @l:u3 but I'm unable to combine the two

I have tried the following without success (fails when trying to format the template with an exception):

level: @l:u3
{'level': @l:u3}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的味道 2025-02-03 03:40:23

在写作时,这是 serilog.expressions 希望能尽快解决。

更新:版本3.4.0-dev-*现在在nuget上支持toString(@l,'u3')

您可以通过有条件地解决它:

{'level': if @l = 'Information' then 'INF' else if @l = 'Warning' then 'WRN' else 'ERR'}

还有一些分支以用于剩余水平。

(另外,您可以写入和插入用户定义的功能沿toshortlevel(@l)的行,然后使用它。

At the time of writing this is a limitation in Serilog.Expressions, which will hopefully be addressed soon.

Update: version 3.4.0-dev-* now on NuGet supports ToString(@l, 'u3').

You can work around it with conditionals:

{'level': if @l = 'Information' then 'INF' else if @l = 'Warning' then 'WRN' else 'ERR'}

With a few more branches for remaining levels.

(Alternatively, you could write and plug in a user defined function along the lines of ToShortLevel(@l), and use that instead.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文