serilog / serilog-sinks-email

A Serilog sink that writes events to SMTP email
Apache License 2.0
76 stars 69 forks source link

Email Sink not sending Email (gmail smtp) #95

Closed nfsiam closed 11 months ago

nfsiam commented 3 years ago

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();
            }
dlausch commented 3 years ago

@nfsiam Were you able to resolve your issue with the Serilog email sink as I'm having the same issue.

norbertmarkiel commented 2 years ago

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); }

prandoz commented 2 years ago

Hi someone managed to solve the issue?

xantari commented 2 years ago

Add this and attach logs, it should indicate why it is not sending an email:

Serilog.Debugging.SelfLog.Enable(Console.WriteLine);

nblumhardt commented 11 months ago

Log.CloseAndFlush() or disposing your root Logger should resolve this without any spin waiting.