Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No logs are being sent #113

Closed
cryo75 opened this issue Feb 24, 2023 · 3 comments
Closed

No logs are being sent #113

cryo75 opened this issue Feb 24, 2023 · 3 comments

Comments

@cryo75
Copy link

cryo75 commented Feb 24, 2023

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": "[email protected]",
            "ToEmail": "[email protected]",
            "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?

@cryo75 cryo75 changed the title No logs as being sent No logs are being sent Feb 24, 2023
@cocowalla
Copy link

cocowalla commented Feb 24, 2023

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
Copy link
Author

cryo75 commented Feb 24, 2023

  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
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants