Search code examples
asp.net-coreserilog

Serilog creates multiple MSSqlServer logs when using multiple environment configuraiton files


I'm using the SQL Server Serilog sink and using different appsettings.<EVN>.json configuration files (one for the default which is appsettings.json and one for the specific environment I am running in which is currently appsettings.test.json). appsettings.json specifies a SQL Server localhost in the connection string and the appsettings.test.json file specifies a different SQL server for the connection string. When running under any environment other than DEVELOPMENT, Serilog will create a LOG table in both the localdb and whatever environment I am testing (TEST_MSSQL in this case). My assumption is that there would only be one log configured and that the server setting in the appsettings.test.json (External SQL Server) would override the value in the default appsettings.json (Local SQL Server), but it appears to be creating configuring in both the localhost and test SQL Servers.

WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .UseSerilog((context, config) => config.ReadFrom.Configuration(Configuration))
    .Build()
    .Run();

Settings in default appsettings.json

"Serilog": {
    "Using": [ "Serilog.Sinks.MSSqlServer" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Error"
      }
    },
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyDB;Integrated Security=True;Connect Timeout=30;",
          "schemaName": "dbo",
          "tableName": "Log",
          "autoCreateSqlTable": true,
          "restrictedToMinimumLevel": "Warning",
          "batchPostingLimit": 1000,
          "period": "0.00:00:30",
          "columnOptionsSection": {
            "addStandardColumns": [ "LogEvent" ],
            "removeStandardColumns": [ "MessageTemplate", "Properties" ],
            "logEvent": {
              "excludeAdditionalProperties": true,
              "excludeStandardColumns": true
            }
          }
        }
      }
    ]
  }

Settings in appsettings.test.json

 "Serilog": {
    "WriteTo": [
      {
        "Args": {
          "connectionString": "Data Source=TEST_MSSQL;Initial Catalog=MyDB;Integrated Security=True;Connect Timeout=30;"
        }
      }
    ]
  }

Solution

  • When running under any environment other than DEVELOPMENT, Serilog will create a LOG table in both the localdb and whatever environment I am testing (TEST_MSSQL in this case).

    I can reproduce same issue based on the configuration data you provided.

    To override default connectionString for Serilog you specified in appsettings.json file and fix above issue, you can update your appsettings.test.json file like below.

    {
      "Serilog": { 
        "WriteTo": [
          {
            "Name": "Console"
          },
          {
            "Name": "MSSqlServer",
            "Args": {
              "connectionString": "Data Source=TEST_MSSQL;Initial Catalog=MyDB;Integrated Security=True;Connect Timeout=30;",
    
            }
          }
        ]
      }
    }