serilog-mssql / serilog-sinks-mssqlserver

A Serilog sink that writes events to Microsoft SQL Server and Azure SQL
Apache License 2.0
283 stars 148 forks source link

Sinks.MSSqlServer bug with SQL Server port on connection string #483

Closed Valerio81 closed 1 year ago

Valerio81 commented 1 year ago

Bug Report

When Serilog try to write into the DB, SQL Server get me the "login failed" error although login data are the same used by the application.

I have "solved" the problem deleting the port number (1433) from the connectionstring. I don't have SQL Server instances that not using default port so I can't check if this problem occours using other port numbers.

Names and versions of all Serilog packages used in the project:

Target framework and operating system: .NET CORE 6

appsettings.json { "ConnectionStrings": { "SqlServer": "Server=127.0.0.1,1433;Database=MyDb;User ID=MyUser;Password=MyPass;MultipleActiveResultSets=true;Max Pool Size=500;Persist Security Info=True;Encrypt=False;TrustServerCertificate=False;" }, "AppSettings": { }, "Logging": { "LogLevel": { "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "Serilog": { "MinimumLevel": "Debug", "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithExceptionDetails" ], "WriteTo": [ { "Name": "MSSqlServer", "Args": { "connectionString": "Server=127.0.0.1,1433;Database=MyDb;User ID=MyUser;Password=MyPass;MultipleActiveResultSets=true;Max Pool Size=500;Persist Security Info=True;Encrypt=False;TrustServerCertificate=False;" "restrictedToMinimumLevel": "Information", "sinkOptionsSection": { "tableName": "Serilog", "schemaName": "dbo", "autoCreateSqlTable": false, "batchPostingLimit": 100 }, "columnOptionsSection": { "removeStandardColumns": [ "MessageTemplate" ], "additionalColumns": [ { "ColumnName": "ApplicationName", "DataType": "nvarchar", "DataLength": 200, "AllowNull": true } ] } } } ], "Properties": { "ApplicationName": "SampleName" } }, "AllowedHosts": "*" }

Program.cs [Main]

Configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddEnvironmentVariables() .Build();

Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(Configuration) .Enrich.WithProperty("ApplicationName", AssemblyName) .CreateLogger();

ckadluba commented 1 year ago

Hello @Valerio81!

I tested with the SQL Server on my local machine using the program in samples/WorkerServiceDemo and had a similar issue.

By default the program uses the connection string (appsettings.json).

Server=localhost;Database=LogTest;Integrated Security=SSPI;Encrypt=False;

This works fine.

I checked on which port my SQL Server was listening and found out it is 1434.

grafik

With the changed connection string

Server=localhost,1434;Database=LogTest;Integrated Security=SSPI;Encrypt=False;

an error occurred. The following exception was thrown.

grafik

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)

Then I changed the connection string to the following but still received the same error.

Server=127.0.0.1;Database=LogTest;Integrated Security=SSPI;Encrypt=False;

Then I tried the same with the SQLCMD commandline utility with the same result.

grafik

As you can see, SQLCMD also can connect when I specify only localhost but not if I specify localhost,1434.

Therefore I tink this effect is related to the configuration of my local SQL Server instance and not to the code of the MSSQL sink.

Here is a SQL Sever connection troubleshooting guide from Microsoft which mentions this error.

https://learn.microsoft.com/en-us/troubleshoot/sql/database-engine/connect/network-related-or-instance-specific-error-occurred-while-establishing-connection#step-2-verify-that-the-sql-server-browser-service-is-running

I'm closing this issue since it is most definitly not a bug in our code.