Serilog - Output/Enrich All Messages with MethodName from which log entry was Called

后端 未结 5 1751
面向向阳花
面向向阳花 2020-12-02 16:24

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

For Instance consider If I have the following;

Public Class MyClassName

  Privat         


        
5条回答
  •  既然无缘
    2020-12-02 16:43

    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!");
    

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

    
    Public Sub Here(ByVal logger as ILogger,
         Optional memberName As String = Nothing)
    
        Return logger.ForContext("MemberName", memberName)
    End Sub 
    

提交回复
热议问题