I have an issue with serilog when i want to write to a database based on different environments. Serilog override my appsettings.production.json and always take the settings in appsettings.json instead even in a production mode !
My code in my local appsettings.json
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": {
"Console" : {"Name": "Console"},
"Sql": {
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=MyServerInDev\\SQLSERVER;Database=MyDBDev;user id=ui;password=pw_;ConnectRetryCount=0;MultipleActiveResultSets=true",
"tableName": "Log",
"autoCreateSqlTable": true
}
}
},
appsettings.production.json
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": {
"Default": "Warning",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": {
"Console" : {"Name": "Console"},
"Sql": {
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=MyServerInProd\\SQLSERVER;Database=MyDBProdv;user id=ui;password=pw_;ConnectRetryCount=0;MultipleActiveResultSets=true",
"tableName": "Log",
"autoCreateSqlTable": true
}
}
},
My program.cs
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables()
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
I am missing something ? and why serilog take the appsettings.json settings and not appsettings.production.json even in prod mode
Note that the other settings in the appsettings.production.json work fine. Only the settings in serilog is causing the problem