Last active
October 10, 2019 12:03
-
-
Save bluewalk/8b8fca3a00697e78d5cb2e88278d87a6 to your computer and use it in GitHub Desktop.
DateTimeLogger: ILogger with DateTime stamp for .NET Core projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// DateTimeLogger | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <inheritdoc /> | |
public class DateTimeLogger<T> : ILogger<T> | |
{ | |
private readonly ILogger _logger; | |
/// <inheritdoc /> | |
public DateTimeLogger(ILogger logger) => _logger = logger; | |
/// <inheritdoc /> | |
public DateTimeLogger(ILoggerFactory loggerFactory) : this(new Logger<T>(loggerFactory)) { } | |
/// <inheritdoc /> | |
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.Now:dd-MM-yyyy HH:mm:ss.fff}]: {formatter(s, ex)}"); | |
/// <inheritdoc /> | |
public bool IsEnabled(LogLevel logLevel) => _logger.IsEnabled(logLevel); | |
/// <inheritdoc /> | |
public IDisposable BeginScope<TState>(TState state) => _logger.BeginScope(state); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment