Search code examples
emailserilog

Trying to Configure Serilog Email sink with appsettings.json to work with Gmail


In in a POC I got the Smtp client to send emails through Gmail, so I know my information regarding connecting to Gmail's SMTP server is correct. I am now trying to configure Serilog through appsettings.json to send my log entries through Gmail. I need to be able to configure it different for different environments. I currently have it set to Verbose so that I get anything...it won't be that way later. I am not getting anything but my file log entry. I had this working with a local network SMTP server that took defaults and no network credentials. Now I need to set the port, ssl, and network credentials to be able to send through Gmail.

Here is my WriteTo section...

 "WriteTo": [      
      {
        "Name": "RollingFile",
        "Args": {
          "pathFormat": "C:/log/log-{Date}.json",
          "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
          "fileSizeLimitBytes": 2147483648,
          "retainedFileCountLimit": 180,
          "restrictedToMinimumLevel": "Verbose"
        }
      },
      {
        "Name": "Email",
        "Args": {
          "connectionInfo": {
            "FromEmail": "{email address}",
            "ToEmail": "{email address}",
            "MailServer": "smtp.gmail.com",
            "EmailSubject": "Fatal Error",
            "NetworkCredentials": {
              "userName": "{gmailuser}@gmail.com",
              "password": "{gmailPassword}"
            },
            "Port": 587,
            "EnableSsl" : true
          },          
          "restrictedToMinimumLevel": "Verbose"
        }
      }
    ]
  },

Any help is appreciated.


Solution

  • Change your port number to 465 and it should work for you. Here's a some info on gmail smtp settings: https://www.lifewire.com/what-are-the-gmail-smtp-settings-1170854

    I'm using Core 2.0 and couldn't get the serilog email sink to work with the appsettings.json file, but I do have it working by setting the configs in the program.cs file like so:

    var logger = new LoggerConfiguration()
    .WriteTo.RollingFile(
        pathFormat: "..\\..\\log\\AppLog.Web-{Date}.txt",
        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}"
    )
    .WriteTo.Email(new EmailConnectionInfo
        {
            FromEmail = appConfigs.Logger.EmailSettings.FromAddress,
            ToEmail = appConfigs.Logger.EmailSettings.ToAddress,
            MailServer = "smtp.gmail.com",
            NetworkCredentials = new NetworkCredential {
                UserName = appConfigs.Logger.EmailSettings.Username,
                Password = appConfigs.Logger.EmailSettings.Password
            },
            EnableSsl = true,
            Port = 465,
            EmailSubject = appConfigs.Logger.EmailSettings.EmailSubject
        },
        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}",
        batchPostingLimit: 10
        , restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error
    )
    .CreateLogger();