BackgroundService Serilog how to email?

Cenk
1,031
Reputation points
Hi,
In my background service, I would like to send error logs. Although there is an error, the email sink does not work. The error is being written to the file and mssqlserver successfully. Can you please share your experiences?
Here is the Program.cs
public class Program
{
public static void Main(string[] args)
{
IHost host = Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureServices(services =>
{
services.AddHttpClient();
services.AddHostedService<Worker>();
})
.Build();
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(host.Services.GetRequiredService<IConfiguration>()).CreateLogger();
host.Run();
}
}
{
"AllowedHosts": "*",
"ConnectionStrings": {
"Default": "Server =.\\SQLEXPRESS; Database=TestAPI;Integrated Security=True; MultipleActiveResultSets=True; Trusted_Connection=yes; Encrypt=False"
},
"Razer": {
"Production": "http://localhost:5236/api/requests"
},
"Serilog": {
"MinimumLevel": "Information",
"Override": {
"Microsoft.AspNetCore": "Warning"
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "Serilogs\\AppLogs.log",
"restrictedToMinimumLevel": "Error",
"buffered": false,
"rollingInterval": "Day",
"retainedFileCountLimit": 3
}
},
{
"Name": "EmailCustom",
"Args": {
"fromEmail": "******@test.com",
"toEmail": "******@gmail.com",
"enableSsl": false,
"mailSubject": "Requests Error",
"isBodyHtml": false,
"mailServer": "smtp.yandex.com.tr",
"networkCredentialuserName": "******@test.com",
"networkCredentialpassword": "123456",
"smtpPort": 587,
"outputTemplate": "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}",
"batchPostingLimit": 1,
"restrictedToMinimumLevel": "Error"
}
},
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server =.\\SQLEXPRESS; Database=TestAPI;Integrated Security=True; MultipleActiveResultSets=True; Trusted_Connection=yes; Encrypt=False",
"tableName": "RequestsExceptionLog",
"autoCreateSqlTable": true,
"restrictedToMinimumLevel": "Error",
"batchPostingLimit": 50,
"period": "0.00:00:20"
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithProcessId",
"WithThreadId"
]
},
"Token": {
"Production": "http://localhost:5236/api/token"
}
}
Sign in to answer