如何实现ILogger的BeginScope

问题描述 投票:0回答:1

如何实现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("你好");

asp.net asp.net-core logging .net-8.0
1个回答
0
投票

参考源码即可

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);
        }
© www.soinside.com 2019 - 2024. All rights reserved.