Serilog - 使用 MethodName 输出/丰富所有消息,从中调用日志条目 [英] Serilog - Output/Enrich All Messages with MethodName from which log entry was Called

查看:79
本文介绍了Serilog - 使用 MethodName 输出/丰富所有消息,从中调用日志条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何都可以使用方法名称来丰富所有 Serilog 输出.

Is there anyway to enrich all Serilog output with the Method Name.

例如考虑如果我有以下内容;

For Instance consider If I have the following;

Public Class MyClassName

  Private Function MyFunctionName() As Boolean
      Logger.Information("Hello World")
      Return True
  End Function

End Class

所需的输出如下;

2015-04-06 18:41:35.361 +10:00 [Information] [MyFunctionName] Hello World!

实际上,完全限定名称会很好.

Actually the Fully Qualified Name would be good.

2015-04-06 18:41:35.361 +10:00 [Information] [MyClassName.MyFunctionName] Hello World!

似乎Enrichers"只对静态信息有用,每次都不起作用.

It seems that "Enrichers" are only good for Static Information and won't work each time.

推荐答案

可以通过反射调用堆栈来使用丰富器来做到这一点,但这样做非常昂贵,因此 Serilog 不提供它.

It's possible to do this with an enricher by reflecting over the call stack, but very expensive to do so, so Serilog doesn't offer it.

相反,您可以使用以下内容:

Instead you can use something like:

Logger.Here().Information("Hello, world!");

并在ILogger上实现Here()方法作为扩展方法:

and implement the Here() method as an extension method on ILogger:

<Extension>
Public Sub Here(ByVal logger as ILogger,
    <CallerMemberName> Optional memberName As String = Nothing)

    Return logger.ForContext("MemberName", memberName)
End Sub 

这篇关于Serilog - 使用 MethodName 输出/丰富所有消息,从中调用日志条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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