2

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()
Matthias Müller
  • 444
  • 6
  • 15
Tom Regan
  • 3,580
  • 4
  • 42
  • 71

2 Answers2

5

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.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • I'm using Serilog.Sinks, which references MailKit 3.1.0, so I don't really have the ability to do what you suggest. The reference is happening inside Serilog.Sinks. – Tom Regan Jan 27 '22 at 22:15
  • Try an older version of Serilig.Sinks that references an older MailKit? – jstedfast Jan 28 '22 at 01:02
  • 4
    @TomRegan [Serilog.Sinks.Email](https://github.com/serilog/serilog-sinks-email/blob/dev/src/Serilog.Sinks.Email/Serilog.Sinks.Email.csproj) references `MailKit 2.6`. What `Serilog.Sinks.xxx` references `MailKit 3+`? FWIW, I just ran across this same issue and downgraded my direct reference of MailKit from 3+ to 2.15; other than the return type difference and some new `IDisposables` , the roll back worked w/o issue. – Metro Smurf Mar 13 '22 at 00:17
2

By default when installing the latest Serilog.Sinks.Email (released on 2020-09-20), it references MailKit.2.6.0

So the email sink will work as long as your MailKit is version 2.6.0 or less. MailKit dependency

Codingwiz
  • 192
  • 2
  • 14