c# - 添加自定义 ILogger 类时安全连接失败
问题描述
我想将我的自定义记录器类添加到我的 API。我创建了一个BitLogger
实现的类ILogger
和一个BitLoggerProvider
实现的类ILoggerProvider
。记录器工作并以所需格式输出信息。
但由于某种原因,当我添加这个 LoggerBitLoggerProvider
时 任何连接到应用程序的浏览器都无法建立安全连接。删除该.AddProvider(...)
声明似乎可以解决此问题。
起初我认为跑步dotnet dev-certs https --trust
可能会解决它。但事实并非如此,也许 Firefox 没有从 Windows 导入证书;但是当尝试为https://localhost:5001/
firefox 添加例外时告诉我没有托管 SSL 证书。
记录器在从提供者的代码创建一个时起作用,但在注入它时不起作用.AddProvider(...)
。
任何帮助表示赞赏!
我的代码:
位记录器
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(); });
}
}
解决方案
所以,这是一个奇怪的问题。
解决方案在BitLogger
课堂上,.BeginScope()
确切地说是在方法中。我忘了实际实现这个方法,但由于某种原因,它没有抛出NotImplementedException
在正文中声明的那个。在任何地方都找不到与此异常相关的堆栈跟踪。也许是因为无法记录异常,因为记录器抛出了异常?
无论如何...更改throw new NotImplementedException();
为return null;
解决此问题。
推荐阅读
- javascript - 为什么 clickHandler 不更新状态?
- jquery - 如何让 jquery 函数与 angularJS 一起工作?
- javascript - pathFinding.js 库 - 只有第一个路径是正确的
- python - 自动编码器未在 keras 中显示预期输出
- selenium-webdriver - 创建 def 检查 URL 是否有效,如果未找到,则转到其他 URL 以编辑该字段,但检查当前 URL,然后提取正确的信息
- flutter - 有没有办法在 Flutter 中重新绘制屏幕或导航到 AppLifecycleState.paused 上的不同屏幕
- python - 从图像中选择区域但可调整大小的 QRubberBand 不会调整大小
- regex - 无法使用正则表达式为特定标签运行 Gitlab CI
- sql - 将两列上的三个表连接起来,其中不匹配的空白处有空格
- accessibility - 如何防止 JAWS 屏幕阅读器在 div 复选框上说“只读”?