Serilog 无法登录我的本地数据库

Chr*_*dez 2 c# database asp.net logging serilog

我正在尝试使用 Serilog 登录我的 .NET 项目,但我注意到错误没有写入数据库。我打开 Serilog 的 SelfLog,这是显示的错误:

2022-11-01T15:58:26.9744639Z Unable to write 1 log events to the database due to following error: A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)

这是我在 Program.cs 中对 Serilog 的设置:

var columnOptions = new ColumnOptions();
        columnOptions.Store.Remove(StandardColumn.Properties);
        columnOptions.Store.Remove(StandardColumn.MessageTemplate);
        columnOptions.TimeStamp.NonClusteredIndex = true;
Log.Logger = new LoggerConfiguration()
                    .ReadFrom.Configuration(Config)
                    .Enrich.FromLogContext()
                    .Enrich.WithExceptionDetails()
                    .WriteTo.Console()
                    .WriteTo.MSSqlServer(
                            connectionString: Config.GetSection("DbConfiguration")["ConnString"],
                            sinkOptions: new MSSqlServerSinkOptions
                            {
                                TableName = Config.GetSection("Serilog")["TableName"],
                                SchemaName = "dbo",
                                AutoCreateSqlTable = false,
                                BatchPeriod = TimeSpan.FromMilliseconds(5)
                            },
                            columnOptions: columnOptions
                        )
                    .CreateLogger();
Run Code Online (Sandbox Code Playgroud)

我已经确认我给 Serilog 的连接字符串与我通常用来访问数据库的连接字符串相同,并且我正在手动创建表,但是即使我将其设置为 true 并让 Serilog 自动创建它,它抛出同样的错误,无法登录。我在网上的其他页面上找到了关于此错误的类似版本,但我找不到涉及 Serilog 的版本,并且其他页面与我正在做的事情无关。

有人可以帮助我了解发生了什么事吗?

mas*_*son 5

从 Serilog.Sinks.MSSqlServer v5 开始,它使用Microsoft.Data.SqlClient而不是 System.Data.SqlClient 与 SQL Server 通信。从 v4 开始,默认启用 Microsoft.Data.SqlClient 加密,并验证用于加密的证书的有效性。您有多种选择:

当您更新自己的代码以使用 Microsoft.Data.SqlClient 而不是 System.Data.SqlClient 时,您可能会遇到相同的问题。现在不妨硬着头皮使用您的机器信任的证书。