Email Sink not sending Email (gmail smtp)
Closed this issue · 5 comments
Hello, I am using the below code, but it is not working, not even giving me any error. I am also using MSSql Sink for DB logging (for that, the config is coming from appSettings.json
), and it is working fine.
I have tried to use the email sink by loading config from appSettings.json
, but it doesn't work either.
Any help is appreciated.
Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.FromLogContext()
.ReadFrom.Configuration(configBuilder)
.WriteTo.Email(new EmailConnectionInfo
{
FromEmail = "*****@gmail.com",
ToEmail = "******@gmail.com",
MailServer = "smtp.gmail.com",
NetworkCredentials = new NetworkCredential("*****@gmail.com", "******"),
EnableSsl = true,
Port = 465,
EmailSubject = "Serilog Log Event"
}, batchPostingLimit: 1)
.CreateLogger();
try
{
Log.Information("Application Starting Up");
CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application Start-Up Failed");
}
finally
{
Log.CloseAndFlush();
}
@nfsiam Were you able to resolve your issue with the Serilog email sink as I'm having the same issue.
In my case, it was just a little wait.
The problem arises when we set a breakpoint on the next method, or sending an email is the last step of the application. For this purpose, I created a very advanced waiting method, which I recommend to copy after calling the logger :)
for (int i = 0; i < 1000000; i++)
{
Console.WriteLine(i);
}
Hi someone managed to solve the issue?
Add this and attach logs, it should indicate why it is not sending an email:
Serilog.Debugging.SelfLog.Enable(Console.WriteLine);
Log.CloseAndFlush()
or disposing your root Logger
should resolve this without any spin waiting.