I'm using .net 8 minimal API and I've added Serilog to my project:
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Sinks.MySQL" Version="5.0.0" />
I'm writing to a MySQL database and if the table doen't exist, I have Serilog create it. Heres appsettings.json:
"WriteTo": [
{
"Name": "MySQL",
"Args": {
"connectionString": "server=myserver;port=3306;Database=mydatabase;uid=username;pwd=password;",
"tableName": "Logs",
"autoCreateSqlTable": true,
"batchPostingLimit": 100,
"period": "0.00:00:10",
When testing, the table didn't exist. I ran my application and the table was created and I saw my logs (21 records).
I ran the application again and tried to write some new logs I added, and nothing was written. The same number of records (21) that were in the table previously are the only ones that are still there. No errors or anything.
I delete the table, run my application again and all logs are written (including the old and new ones). Here is how I initialize Serilog in Program.cs:
var builder = WebApplication.CreateBuilder(args);
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
builder.Host.UseSerilog((context, config) =>
{
config
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext();
});
builder.Services.AddEndpointsApiExplorer();
Why do my logs only get written if the table doesn't exist and how it I configure Serilog to write to my database everytime regardless if the table existed or not?
Thanks
EDIT: I added the following to appsettings.json:
"autoCreateSqlDatabase": false,
"autoCreateSqlTable": false,
This is how I log (simple):
logger.Information("This should be in the database");
Still, no new records are written to the database.
appsettings.json
Program.cs
WeatherForecastController controller