Search code examples
c#azure-sql-databaseserilogasp.net-core-2.1

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


I have an ASP.Net Core 2.1 web API that is connecting to Azure SQL Server for the backing data store with no problems.

I am currently trying to set up Serilog so that it writes errors to an "ApiLog" table in my Azure SQL Server database. I am using the MSSqlServer sink for Serilog and following the setup described in this blog. I use the following code in my Program.cs to see any Serilog errors;

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

... and am getting this error;

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

I do not have any problem connecting to the Azure SQL Server (same server and database but different database tables) in my API's DB Context classes so I am not sure what the issue is. I am using the same SQL login account for my DB Context connections as I am using for Serilog.

Here is my writeto section in appsettings.json;

"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"
    }
  },

I even tried setting the User ID and User Password to the SA login as a test and got the same error so I do not think it could be a permissions SQL Server issue.

So, I am wondering... is the Serilog MSSqlServer sink actually supported with Azure SQL Server? So far I have not been able to find anything specific on the internets that says one way or the other.

Any ideas?


Solution

  • I just tried it in a local console app using the documentation as a guide and it worked fine.

    // 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");
    

    There was no connection errors.

    Documentation example: https://github.com/serilog/serilog-sinks-mssqlserver#code-net-framework

    I made the table myself, I didn't try the auto create: https://github.com/serilog/serilog-sinks-mssqlserver#table-definition

    Here's the row in the table:

    Id Message MessageTemplate Level TimeStamp Exception Properties

    1 It works It works Error 2018-09-28 17:11:04.1189594 +01:00 NULL <properties />