serilog / serilog-sinks-email

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

No logs are being sent #113

Closed cryo75 closed 1 year ago

cryo75 commented 1 year ago

I have a NET7 WebApi project and I'm using the latest prerelease package (2.4.1-dev-00147). I configured the appsettings.json as follows:


  "Serilog": {
    "Using": [ "Serilog.Sinks.Email", "MyApi" ],
    "MinimumLevel": {
      "Default": "Debug"
    },
    "WriteTo": [
      {
        "Name": "CustomEmail",
        "Args": {
          "ConnectionInfo": {
            "FromEmail": "fromemail@server.com",
            "ToEmail": "toemail@server.com",
            "MailSubject": "{Message}",
            "IsBodyHtml": false,
            "EnableSsl": true,
            "MailServer": "mail.server.com",
            "Port": "587",
            "NetworkCredentials": {
              "UserName": "username",
              "Password": "password"
            }
          },
          "OutputTemplate": "Error occured at - {Timestamp:HH:mm:ss}\nError Message :{Message:lj} ",
          "RestrictedToMinimumLevel": "Error"
        }
      }
    ]
  }

And also created this extension:

    public static class SerilogEmailExtension
    {
        const string DefaultOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}";

        public static LoggerConfiguration CustomEmail(
            this LoggerSinkConfiguration loggerConfiguration,
            CustomEmailConnectionInfo connectionInfo,
            string outputTemplate = DefaultOutputTemplate,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
        )
        {
            return loggerConfiguration.Email(
                connectionInfo,
                outputTemplate,
                restrictedToMinimumLevel
            );
        }

        public class CustomEmailConnectionInfo : EmailConnectionInfo
        {
            public CustomEmailConnectionInfo()
            {
                NetworkCredentials = new NetworkCredential();
            }
        }
    }

But no email is being sent. Am I missing something?

cocowalla commented 1 year ago

A few questions/things to check:

  1. Does it work if you don't use a custom extension method?
  2. What namespace is your extension method in?
  3. If you set a breakpoint in your extension method, is it being hit?
  4. Have you tried using SelfLog to expose any internal errors that might be occurring? (https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics#selflog)
cryo75 commented 1 year ago
  1. No it doesn't work without the custom extension.
  2. It is in the same namespace as the main project.
  3. Yes. It is being hit at startup and all the settings are set correctly.
  4. Thanks for the tip. I added that and I got:

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.MailKitEmailTransport.SendMailAsync(EmailMessage emailMessage) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at Serilog.Sinks.Email.MailKitEmailTransport.SendMailAsync(EmailMessage emailMessage) at Serilog.Sinks.Email.EmailSink.EmitBatchAsync(IEnumerable`1 events) at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.OnTick()

cocowalla commented 1 year ago

This seems to be an issue with the version of MailKit that this sink uses - I believe this is the same issue as #106 and #107, so I'll close this one out and suggest you subscribe to notifications for #106 and #107.

In the meantime, something to try is explicitly bringing in MailKit to your project (instead of as a transitive dependency via this sink), and trying an older version. This person had success with 2.15. Hope that helps!