Serilog WriteTo.Email() failing

Viewed 451

This is a .Net Core 6 console application that uses Serilog.Sinks.Email 2.4.0. In the same project I am referencing the MailKit 3.1.0 Nuget package. I notice that Serilog.Sinks.Email also references Mailkit 3.1.0.

I'm configuring the Logger like so. My goal is to have Serilog send an email when Log.Fatal() is invoked:

.WriteTo.Email(  
    new EmailConnectionInfo()
    {
        FromEmail = _appSettings.EmailFrom,
        ToEmail = _appSettings.EmailToOnError,
        MailServer = _appSettings.EmailHost,
        EmailSubject = $"Runtime error starting {_appSettings.ApplicationName}",
        ServerCertificateValidationCallback = (s, c, h, e) => true
    },
    restrictedToMinimumLevel: LogEventLevel.Fatal);

I am logging Serilog messages to the Debug output window, like so, so I can see the errors messages that Serilog would otherwise (by design) swallow:

Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));

When I call Log.Fatal(), followed immediately by Log.CloseAndFlush(), and look in the Debug output window, I can see that Serilog tries to send me an email message, but fails, saying that it cannot find the method MailKit.MailTransport.SendAsync(). Any idea what I am missing? Here is the full error message from the Debug output window:

Exception while emitting periodic batch from 
Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink: System.MissingMethodException: 
Method not found: 'System.Threading.Tasks.Task 
MailKit.MailTransport.SendAsync(MimeKit.MimeMessage, System.Threading.CancellationToken, 
MailKit.ITransferProgress)'.
   at Serilog.Sinks.Email.EmailSink.EmitBatchAsync(IEnumerable`1 events)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine] 
   (TStateMachine& stateMachine)
   at Serilog.Sinks.Email.EmailSink.EmitBatchAsync(IEnumerable`1 events)
   at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.OnTick()
1 Answers

MailKit's SmtpClient.SendAsync() method changed its API signature between 2.x and 3.0. It used to return Task, now it returns Task<string>.

This causes libraries built against 2.x to fail to be able to find the method in the 3.0 (or 3.1) assembly.

Downgrade your MailKit to 2.15 (or 2.15.1) and it will likely work.

Related