Serilog 动态添加自定义属性

注:通过 using 的使用,可以在函数结束时自动移除属性 CallId。

代码示例:

public bool UsbRegRead(uint regAddr, ref uint regData)
{
  using (LogContext.PushProperty("CallId", Guid.NewGuid().ToString()))
  {
    Log.Information("UsbRegRead() <<");
    
    // ... 具体逻辑

    Log.Information("UsbRegRead() >>");

  }

}

参考如下:

  

参考:官方教程

  

原文地址:https://www.cnblogs.com/dhqy/p/14704405.html