Skip to content

Instantly share code, notes, and snippets.

@Powerz
Created August 16, 2018 13:03
Show Gist options
  • Save Powerz/a51744332b2cd915d3ad60354b6bf8e8 to your computer and use it in GitHub Desktop.
Save Powerz/a51744332b2cd915d3ad60354b6bf8e8 to your computer and use it in GitHub Desktop.
ILogger<T> for old ASP.NET (not Core)
// Bind ILoggerFactory to LoggerFactory
// Bind typeof(ILogger<>) to typeof(LoggingAdapter<>)
public class LoggingAdapter<T> : ILogger<T>
{
private readonly Microsoft.Extensions.Logging.ILogger _logger;
public LoggingAdapter(ILoggerFactory factory)
{
_logger = factory.CreateLogger<T>();
}
public IDisposable BeginScope<TState>(TState state)
{
return _logger.BeginScope(state);
}
public bool IsEnabled(LogLevel logLevel)
{
return _logger.IsEnabled(logLevel);
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
_logger.Log(logLevel, eventId, state, exception, formatter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment