繁体   English   中英

Serilog MSSqlServer接收器是否应该与Azure SQL Server一起使用?

[英]Is Serilog MSSqlServer sink supposed to work with Azure SQL Server?

我有一个ASP.Net Core 2.1 Web API,它可以毫无问题地连接到Azure SQL Server作为后备数据存储。

我当前正在尝试设置Serilog,以便它将错误写入我的Azure SQL Server数据库中的“ ApiLog”表。 我正在为Serilog使用MSSqlServer接收器,并遵循此博客中描述的设置。 我在Program.cs中使用以下代码来查看任何Serilog错误;

        Serilog.Debugging.SelfLog.Enable(msg =>A
        {
            Debug.Print(msg);
            Debugger.Break();
        });

...并收到此错误;

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 pre-login handshake. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host

我在API的数据库上下文类中连接到Azure SQL Server(服务器和数据库相同,但数据库表不同)时没有任何问题,所以我不确定问题出在哪里。 我为数据库上下文连接使用的SQL登录帐户与为Serilog使用的SQL登录帐户相同。

这是我在appsettings.json中的writeto部分;

"WriteTo": [
  {
    "Name": "MSSqlServer",
    "Args": {
      "connectionString": "Server=<server name in Azure>.database.windows.net,1433;Initial Catalog=Logs;Persist Security Info=False;User ID=<SQL account name>;Password=<Sql Account Password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
      "tableName": "ApiLog"
    }
  },

我什至尝试将用户ID和用户密码设置为SA登录作为测试,并得到相同的错误,因此我认为这可能不是SQL Server的权限问题。

所以,我想知道... Azure SQL Server是否真正支持Serilog MSSqlServer接收器? 到目前为止,我还无法在互联网上找到任何一种可以说是另一种方式的具体信息。

有任何想法吗?

我只是在本地控制台应用程序中以文档为指南进行了尝试,但效果很好。

// This doesn't break/I don't get any exceptions.
Serilog.Debugging.SelfLog.Enable(msg =>
{
    Debug.Print(msg);
    Debugger.Break();
});

var tableName = "Logs";
var connectionString = "Server=SERVER;Database=DATABASE;User Id=USERNAME;Password=PASSWORD;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Persist Security Info=False;";

var log = new LoggerConfiguration()
    .WriteTo.MSSqlServer(connectionString, tableName)
    .CreateLogger();

log.Write(Serilog.Events.LogEventLevel.Error, "It works");

没有连接错误。

文档示例: https : //github.com/serilog/serilog-sinks-mssqlserver#code-net-framework

我自己制作了表格,但没有尝试自动创建: https : //github.com/serilog/serilog-sinks-mssqlserver#table-definition

这是表中的行:

ID消息MessageTemplate级别TimeStamp异常属性

1可行可行错误2018-09-28 17:11:04.1189594 +01:00 NULL <properties />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM