serilog-mssql / serilog-sinks-mssqlserver

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

`AutoCreateSqlDatabase` should not log an error if the DB already exists #478

Closed cremor closed 1 year ago

cremor commented 1 year ago

Please clearly describe what the SQL Sink is doing incorrectly:

If you enable the AutoCreateSqlDatabase option and the DB already exists this sink will write an exception to the Serilog self log each time the application is started.

Please clearly describe the expected behavior:

No "create DB" action should be executed if the DB already exists. If this is not possible, at least the "Database 'Xyz' already exists." exception should be handled and not logged.

List the names and versions of all Serilog packages used in the project:

Target framework and operating system:

.NET 7 OS: Windows 10

Provide a simple reproduction of your Serilog configuration code:

var builder = WebApplication.CreateBuilder(args);
// configure services ...

builder.Host.UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
    .ReadFrom.Configuration(context.Configuration));

var app = builder.Build();
// ...

Provide a simple reproduction of your Serilog configuration file, if any:

  "Serilog": {
    "WriteTo": {
      "DbSink": {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "DefaultConnection",
          "sinkOptionsSection": {
            "tableName": "Logs",
            "autoCreateSqlTable": true,
            "AutoCreateSqlDatabase":  true
          }
        }
      }
    }
  }

Provide a simple reproduction of your application code:

Not required. The error is logged as soon as builder.Build() is called.

ckadluba commented 1 year ago

Hi @cremor!

Thanks for bringing this up. I will take a look at this.