首页 > 解决方案 > 添加自定义 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(); });
        }
    }

标签: c#logging.net-core

解决方案


所以,这是一个奇怪的问题。

解决方案在BitLogger课堂上,.BeginScope()确切地说是在方法中。我忘了实际实现这个方法,但由于某种原因,它没有抛出NotImplementedException在正文中声明的那个。在任何地方都找不到与此异常相关的堆栈跟踪。也许是因为无法记录异常,因为记录器抛出了异常?

无论如何...更改throw new NotImplementedException();return null;解决此问题。


推荐阅读