使用.net core 3.1和serilog sql server sink,数据不会写入数据库。 在这里启用 serilog selflog 是我的控制台上出现以下错误:
HTTPS 连接验证失败。 System.IO.IOException:身份验证失败,因为远程方已关闭传输流。
手动创建数据库并启动应用程序后,会在数据库内创建“ErrorLog”表(使用 serilog 配置),但在插入日志时会发生这种情况。
更新
无论我使用什么水槽,我都会遇到同样的错误!
[17:14:10 INF] Hello, world!
[17:14:10 DBG] Hosting starting
[17:14:10 DBG] Failed to locate the development https certificate at 'null'.
[17:14:10 DBG] Using development certificate: CN=localhost (Thumbprint: 042981E3AFFFDD00DE844A41D4437C2A52529AEB)
[17:14:10 INF] Now listening on: https://localhost:5001
[17:14:10 INF] Now listening on: http://localhost:5000
[17:14:10 DBG] Loaded hosting startup assembly demo-log
[17:14:10 INF] Application started. Press Ctrl+C to shut down.
[17:14:10 INF] Hosting environment: Development
[17:14:10 INF] Content root path: C:\Users\Omid\source\repos\demo-log\demo-log
[17:14:10 DBG] Hosting started
[17:14:11 DBG] Connection id "0HM29NMTJB5T6" received FIN.
[17:14:11 DBG] Connection id "0HM29NMTJB5T7" received FIN.
[17:14:11 DBG] Connection id "0HM29NMTJB5T7" accepted.
[17:14:11 DBG] Connection id "0HM29NMTJB5T6" accepted.
[17:14:11 DBG] Connection id "0HM29NMTJB5T7" started.
[17:14:11 DBG] Connection id "0HM29NMTJB5T6" started.
[17:14:11 DBG] Failed to authenticate HTTPS connection.
System.IO.IOException: Authentication failed because the remote party has closed the transport stream.
at System.Net.Security.SslStream.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.ProcessAuthentication(LazyAsyncResult lazyResult, CancellationToken cancellationToken)
at System.Net.Security.SslStream.BeginAuthenticateAsServer(SslServerAuthenticationOptions sslServerAuthenticationOptions, CancellationToken cancellationToken, AsyncCallback asyncCallback, Object asyncState)
at System.Net.Security.SslStream.<>c.<AuthenticateAsServerAsync>b__69_0(SslServerAuthenticationOptions arg1, CancellationToken arg2, AsyncCallback callback, Object state)
at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl[TArg1,TArg2](Func`5 beginMethod, Func`2 endFunction, Action`1 endAction, TArg1 arg1, TArg2 arg2, Object state, TaskCreationOptions creationOptions)
at System.Threading.Tasks.TaskFactory.FromAsync[TArg1,TArg2](Func`5 beginMethod, Action`1 endMethod, TArg1 arg1, TArg2 arg2, Object state, TaskCreationOptions creationOptions)
at System.Threading.Tasks.TaskFactory.FromAsync[TArg1,TArg2](Func`5 beginMethod, Action`1 endMethod, TArg1 arg1, TArg2 arg2, Object state)
at System.Net.Security.SslStream.AuthenticateAsServerAsync(SslServerAuthenticationOptions sslServerAuthenticationOptions, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Https.Internal.HttpsConnectionMiddleware.InnerOnConnectionAsync(ConnectionContext context)
[17:14:11 DBG] Failed to authenticate HTTPS connection.
System.IO.IOException: Authentication failed because the remote party has closed the transport stream.
at System.Net.Security.SslStream.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.ProcessAuthentication(LazyAsyncResult lazyResult, CancellationToken cancellationToken)
at System.Net.Security.SslStream.BeginAuthenticateAsServer(SslServerAuthenticationOptions sslServerAuthenticationOptions, CancellationToken cancellationToken, AsyncCallback asyncCallback, Object asyncState)
at System.Net.Security.SslStream.<>c.<AuthenticateAsServerAsync>b__69_0(SslServerAuthenticationOptions arg1, CancellationToken arg2, AsyncCallback callback, Object state)
at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl[TArg1,TArg2](Func`5 beginMethod, Func`2 endFunction, Action`1 endAction, TArg1 arg1, TArg2 arg2, Object state, TaskCreationOptions creationOptions)
at System.Threading.Tasks.TaskFactory.FromAsync[TArg1,TArg2](Func`5 beginMethod, Action`1 endMethod, TArg1 arg1, TArg2 arg2, Object state, TaskCreationOptions creationOptions)
at System.Threading.Tasks.TaskFactory.FromAsync[TArg1,TArg2](Func`5 beginMethod, Action`1 endMethod, TArg1 arg1, TArg2 arg2, Object state)
at System.Net.Security.SslStream.AuthenticateAsServerAsync(SslServerAuthenticationOptions sslServerAuthenticationOptions, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Https.Internal.HttpsConnectionMiddleware.InnerOnConnectionAsync(ConnectionContext context)
[17:14:11 DBG] Connection id "0HM29NMTJB5T7" stopped.
[17:14:11 DBG] Connection id "0HM29NMTJB5T6" stopped.
[17:14:11 DBG] Connection id "0HM29NMTJB5T6" sending FIN because: "The Socket transport's send loop completed gracefully."
[17:14:11 DBG] Connection id "0HM29NMTJB5T7" sending FIN because: "The Socket transport's send loop completed gracefully."
[17:14:11 DBG] Connection id "0HM29NMTJB5T8" accepted.
[17:14:11 DBG] Connection id "0HM29NMTJB5T8" started.
[17:14:11 VRB] Connection id "0HM29NMTJB5T8" sending SETTINGS frame for stream ID 0 with length 18 and flags NONE
[17:14:11 VRB] Connection id "0HM29NMTJB5T8" sending WINDOW_UPDATE frame for stream ID 0 with length 4 and flags 0x0
[17:14:11 VRB] Connection id "0HM29NMTJB5T8" received SETTINGS frame for stream ID 0 with length 24 and flags NONE
[17:14:11 VRB] Connection id "0HM29NMTJB5T8" sending SETTINGS frame for stream ID 0 with length 0 and flags ACK
[17:14:11 VRB] Connection id "0HM29NMTJB5T8" received WINDOW_UPDATE frame for stream ID 0 with length 4 and flags 0x0
[17:14:11 VRB] Connection id "0HM29NMTJB5T8" received HEADERS frame for stream ID 1 with length 341 and flags END_STREAM, END_HEADERS, PRIORITY
[17:14:11 VRB] Connection id "0HM29NMTJB5T8" received SETTINGS frame for stream ID 0 with length 0 and flags ACK
[17:14:11 INF] Request starting HTTP/2 GET https://localhost:5001/
[17:14:11 DBG] Wildcard detected, all requests with hosts will be allowed.
[17:14:11 VRB] All hosts are allowed.
[17:14:11 DBG] No candidates found for the request path '/'
[17:14:11 DBG] Request did not match any endpoints
[17:14:11 INF] HTTP GET / responded 404 in 16.8270 ms
[17:14:11 VRB] Connection id "0HM29NMTJB5T8" sending HEADERS frame for stream ID 1 with length 71 and flags END_STREAM, END_HEADERS
使用Serilog登录MSSQL数据库。请使用以下步骤。
using Serilog;
using Serilog.Sinks.MSSqlServer;
using System.Diagnostics;
try
{
// This doesn't break/I don't get any exceptions.
Serilog.Debugging.SelfLog.Enable(msg =>
{
Debug.Print(msg);
Debugger.Break();
});
var serLog = new LoggerConfiguration()
.WriteTo.MSSqlServer("yourconnectionstring",
"yourtablename")
.CreateLogger();
Log.Logger = serLog;
Log.Information("It works");
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
finally
{
Log.CloseAndFlush();
}
只需编辑下面的配置即可;
Logger log = new LoggerConfiguration()
.WriteTo.MSSqlServer(
connectionString: "server=SOYER\\SQLEXPRESS;initial catalog=yourDB;TrustServerCertificate=True;integrated security=true",
sinkOptions: new MSSqlServerSinkOptions { TableName = "LogEvents", AutoCreateSqlTable = true })
.CreateLogger();
重要的部分是“TrustServerCertificate = True;集成安全性= true”