1

I am trying to use Serilog's Email sink as part of simple console application. This application has very short lifetime: it checks DB and updates subscribers with a new data. I want to send an email if error happens.

The problem is that if I don't put Thread.Sleep at the end of the app, emails are not sent.

Is there any way to purge email queue and force email to be sent asap?

My code is:

    Log.Logger = new LoggerConfiguration()
        .WriteTo.ColoredConsole()
        .WriteTo.EventLog("LOGSOURCE", restrictedToMinimumLevel: LogEventLevel.Warning)
        .WriteTo.Email(connectionInfo: new EmailConnectionInfo()
        {
            EmailSubject = "App error",
            ToEmail = "support@company.com",
            MailServer = "smtp.office365.com",
            NetworkCredentials = new NetworkCredential("sender@company.com", "Password"),
            Port = 587,
            FromEmail = "sender@company.com", 
            EnableSsl = false

        },
        batchPostingLimit: 1,
        restrictedToMinimumLevel: LogEventLevel.Error)
        .CreateLogger();

Log.Error("Error message");

Any help is appreciated

3 Answers3

1

You could try debug Serilog with Serilog.Debugging.SelfLog.Out = Console.Out

I think you should use EnableSsl = true with o365 and leave batchPostingLimit as default = 100

Boris Lipschitz
  • 9,236
  • 5
  • 53
  • 63
0

Depending on the sink (I'm not certain about email) it is possible that disposing the logger will have the right effect:

((IDisposable)Log.Logger).Dispose();

Failing that, there may be additional work required in the email sink to ensure things are flushed properly (raising a ticket on the Serilog tracker would help with this).

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
0

Have you tried the following?

Log.CloseAndFlush();
Christna
  • 154
  • 1
  • 10