Open dailyenergy opened 9 months ago
Try this, also see https://github.com/serilog/serilog-sinks-email/issues/130
{
"Serilog": {
"MinimumLevel": {
"Default": "Information"
},
"WriteTo": [
{
"Name": "Email",
"Args": {
"options": {
"subject": "Serilog test",
"from": "from@email.com",
"to": [ "to@email.com" ],
"host": "localhost",
"body": "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message}{NewLine}{Exception}{NewLine}"
},
"batchingOptions": {
"batchSizeLimit": 10,
"period": "00:00:01"
},
"restrictedToMinimumLevel": "Information"
}
}
]
}
}
Thanks for your help @MrPsi. I have pretty much adapted your config in my appsettings.json but I am getting one email per logged message. The batching is not working.
I get batching to work when configuring a logger in the app,
await using var log = new LoggerConfiguration()
.WriteTo.Email(
options: new()
{
From = "app2@example.com",
To = new List<string> { "support2@example.com" },
Host = "localhost",
},
batchingOptions: new()
{
BatchSizeLimit = 10,
BufferingTimeLimit = TimeSpan.FromSeconds(30),
})
.CreateLogger();
log.Information("Hello from Serilog!");
log.Information("Hello again from Serilog!");
but not from appsettings.json.
Any clues as to what I'm missing?
"Serilog": {
"Using": [ "Serilog.Sinks.Email", "Serilog.Sinks.Console", "App", "Serilog.Sinks.File" ],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:/bin/Apps/Logs/log.txt",
"rollingInterval": "Day",
"retainedFileCountLimit": "45"
}
},
{
"Name": "Email",
"Args": {
"options": {
"subject": "App Error(s)",
"from": "postmaster@App.com",
"to": [ "tester@test.org" ],
"host": "localhost",
"body": "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message}{NewLine}{Exception}{NewLine}"
},
"batchingOptions": {
"batchSizeLimit": 10,
"period": "00:00:01"
},
"restrictedToMinimumLevel": "Error"
}
}
]
}
Perahps this will help .Net Core and Serilog Email sink - JSON Config
by browsing through source code and other issues I have figured out that I can add Serilog Mail sink in appsettings as follows:
But I have no clue how to set BatchSizeLimit. Do I really need to create a custom extension for LoggerSinkConfiguration?