如何实现ILogger的BeginScope
这样我就可以在 Log 函数中提取发送到 BeginScope 的值
public sealed class MyLogger : ILogger
{
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
// what to do here
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
// so i can get the value of the string App1 here
}
}
使用(logger.BeginScope(“app1”))
logger.LogInformation("你好");
参考源码即可
Logger
private readonly ILogger _logger;
IDisposable? ILogger.BeginScope<TState>(TState state)
{
return _logger.BeginScope(state);
}
void ILogger.Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
_logger.Log(logLevel, eventId, state, exception, formatter);
}