csharp TimedLogger ASP.NET CORE

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp TimedLogger ASP.NET CORE相关的知识,希望对你有一定的参考价值。

using System;
using Microsoft.Extensions.Logging;

public class TimedLogger<T>: ILogger<T>
{
    private readonly ILogger _logger;

    public TimedLogger(ILogger logger) => _logger = logger;

    public TimedLogger(ILoggerFactory loggerFactory): this(new Logger<T>(loggerFactory)) { }

    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) =>
        _logger.Log(logLevel, eventId, state, exception, (s, ex) => $"[{DateTime.UtcNow:HH:mm:ss.fff}]: {formatter(s, ex)}");

    public bool IsEnabled(LogLevel logLevel) => _logger.IsEnabled(logLevel);

    public IDisposable BeginScope<TState>(TState state) => _logger.BeginScope(state);
}

以上是关于csharp TimedLogger ASP.NET CORE的主要内容,如果未能解决你的问题,请参考以下文章