Microsoft -> DependencyInjection
This commit is contained in:
15
DTLib.Logging/DependencyInjection/LoggerService.cs
Normal file
15
DTLib.Logging/DependencyInjection/LoggerService.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace DTLib.Logging.DependencyInjection;
|
||||
|
||||
public class LoggerService<TCaller> : ServiceDescriptor
|
||||
{
|
||||
DTLib.Logging.New.ILogger _logger;
|
||||
|
||||
public LoggerService(DTLib.Logging.New.ILogger logger) : base(
|
||||
typeof(Microsoft.Extensions.Logging.ILogger<TCaller>),
|
||||
new MyLoggerWrapper<TCaller>(logger))
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
}
|
||||
39
DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs
Normal file
39
DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using DTLib.Logging.New;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DTLib.Logging.DependencyInjection;
|
||||
|
||||
public class MyLoggerWrapper<TCaller> : Microsoft.Extensions.Logging.ILogger<TCaller>
|
||||
{
|
||||
private DTLib.Logging.New.ILogger _logger;
|
||||
public MyLoggerWrapper(DTLib.Logging.New.ILogger logger)=>
|
||||
_logger = logger;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
||||
{
|
||||
string message = formatter(state, exception);
|
||||
_logger.Log(nameof(TCaller), LogSeverity_FromLogLevel(logLevel), message);
|
||||
}
|
||||
|
||||
private bool _isEnabled=true;
|
||||
public bool IsEnabled(LogLevel logLevel) => _isEnabled;
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
static LogSeverity LogSeverity_FromLogLevel(LogLevel l)
|
||||
=> l switch
|
||||
{
|
||||
LogLevel.Trace => LogSeverity.Debug,
|
||||
LogLevel.Debug => LogSeverity.Debug,
|
||||
LogLevel.Information => LogSeverity.Info,
|
||||
LogLevel.Warning => LogSeverity.Warn,
|
||||
LogLevel.Error => LogSeverity.Error,
|
||||
LogLevel.Critical => LogSeverity.Error,
|
||||
LogLevel.None => throw new NotImplementedException("LogLevel.None is not supported"),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(l), l, null)
|
||||
}
|
||||
;
|
||||
}
|
||||
Reference in New Issue
Block a user