Я хочу добавить свой собственный класс логгера в мой API. Я создал BitLogger
класс, который реализует ILogger
, и BitLoggerProvider
, который реализует ILoggerProvider
. Регистратор работает и выводит информацию в требуемом формате.
Но по какой-то причине, когда я добавляю этот Регистратор с BitLoggerProvider
; любой браузер, подключающийся к приложению, не может установить безопасное соединение. Удаление оператора .AddProvider(...)
, кажется, исправляет это.
Сначала я подумал, что запуск dotnet dev-certs https --trust
может исправить это. Но это был не тот случай, возможно firefox не импортировал сертификат из windows; но при попытке добавить исключение для https://localhost:5001/
firefox сообщает, что сертификат SSL не размещен.
Регистратор работает при создании сертификата из кода от поставщика, но не при его внедрении с помощью .AddProvider(...)
.
Любая помощь приветствуется!
Мой код:
BitLogger
public class BitLogger : ILogger
{
private readonly List<LogLevel> _levels = new List<LogLevel>()
{
LogLevel.Trace,
LogLevel.Debug,
LogLevel.Information,
LogLevel.Warning,
LogLevel.Error,
LogLevel.Critical
};
private readonly string _root;
private readonly IConfiguration _config;
public BitLogger(string root, IConfiguration config)
{
_root = root;
_config = config;
}
public bool IsEnabled(LogLevel logLevel)
{
if (logLevel == LogLevel.None)
{
return false;
}
return _levels.IndexOf(logLevel) >= _levels.IndexOf(LogLevel.Trace); // TODO
}
public IDisposable BeginScope<TState>(TState state)
{
throw new NotImplementedException();
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
// The given level is to low to print.
if (!IsEnabled(logLevel))
{
return;
}
// The events do not match.
if (_config.GetValue<int>("EventId") != 0 && _config.GetValue<int>("EventId") != eventId.Id)
{
return;
}
_Log(logLevel, eventId, state, exception, formatter);
}
private void _Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var time = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss.fff");
Console.ForegroundColor = logLevel.Color();
Console.Write($"{time} [{logLevel.Abbreviation()}]");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write($" ({eventId.Id}) {_root}".PadRight(_config.GetValue<int>("Padding"), '.'));
Console.WriteLine($" | {formatter(state, exception)}");
}
}
public static class LogLevelExtensions
{
public static string Abbreviation(this LogLevel level)
{
return level switch
{
LogLevel.Critical => "CRI",
LogLevel.Error => "ERR",
LogLevel.Warning => "WRN",
LogLevel.Information => "INF",
LogLevel.Debug => "DBG",
LogLevel.Trace => "TRC",
_ => "???"
};
}
public static ConsoleColor Color(this LogLevel level)
{
return level switch
{
LogLevel.Critical => ConsoleColor.DarkRed,
LogLevel.Error => ConsoleColor.Red,
LogLevel.Warning => ConsoleColor.Yellow,
LogLevel.Information => ConsoleColor.Green,
LogLevel.Debug => ConsoleColor.DarkCyan,
LogLevel.Trace => ConsoleColor.White,
_ => ConsoleColor.Gray
};
}
}
BitLoggerProvider
public class BitLoggerProvider : ILoggerProvider
{
private readonly IConfiguration _config;
private readonly ConcurrentDictionary<string, BitLogger> _loggers = new ConcurrentDictionary<string, BitLogger>();
public BitLoggerProvider(IConfiguration config)
{
_config = config;
}
public ILogger CreateLogger(string categoryName)
{
return _loggers.GetOrAdd(categoryName, name => new BitLogger(name, _config));
}
public void Dispose()
{
_loggers.Clear();
}
}
Запуск
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowOrigin", builder =>
{
builder
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin();
});
});
services.AddControllers();
services.AddLogging(builder =>
{
builder
.ClearProviders()
.AddDebug()
.AddProvider(new BitLoggerProvider(Configuration));
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}