serilogserilog-aspnetcore

Serilog how to send email to multiple recipients


I am working on a background worker service and trying to send emails to multiple recipients with the configuration below. But unfortunately, It does not work. Is there a way to send email to multiple recipients? I searched Google but couldn't find a way through. (serilog aspnet core 6.1 and email sink 2.4)

{
        "Name": "CustomEmail",
        "Args": {
          "ConnectionInfo": {
            "NetworkCredentials": {
             
              "UserName": "support@support.com",
              "Password": "1234"
            },
            "FromEmail": "support@support.com",
            "MailServer": "smtp.gmail.com",
            "EmailSubject": "[{Level}] Multiple Requests",
            "Port": "465",
            "IsBodyHtml": false,
            "EnableSsl": true,
            "ToEmail": "to1@gmail.com; to2@gmail.com",
            "RestrictedToMinimumLevel": "Error",
            "OutputTemplate": "{Timestamp:yyyy-MM-dd HH:mm} [{Level}] {Message}{NewLine}{Exception}"
          }

        }
      }

Both of the usages below did not work. No errors and no emails. By the way, sending an email to only one recipient works.

"ToEmail": "to1@gmail.com; to2@gmail.com"
"ToEmail": "to1@gmail.com;to2@gmail.com"
"ToEmail": "to1@gmail.com, to2@gmail.com"
"ToEmail": "to1@gmail.com,to2@gmail.com"

Solution

  • This post helped me solve my problem.

    Serilog Email Sink

    "Serilog": {
      "Using": [ "Serilog.Sinks.Email", "PurchaseRazerCodesMultipleRequestService", "Serilog.Sinks.File", "Serilog.Sinks.Console", "Serilog.Sinks.MSSqlServer" ],
      "Override": {
        "Microsoft.AspNetCore": "Warning"
      },
    
    
    {
      "Name": "CustomEmail",
      "Args": {
        "ConnectionInfo": {
          "NetworkCredentials": {
            "UserName": "support@support.com",
            "Password": "123456"
          },
          "FromEmail": "support@support.com",
          "MailServer": "smtp.gmail.com",
          "EmailSubject": "[{Level}] Test Requests",
          "Port": "465",
          "IsBodyHtml": false,
          "EnableSsl": true,
          "ToEmail": "test1@gmail.com, test2@gmail.com",
          "RestrictedToMinimumLevel": "Error",
          "OutputTemplate": "{Timestamp:yyyy-MM-dd HH:mm} [{Level}] {Message}{NewLine}{Exception}"
        }
    
      }
    }
    
    public static class SerilogCustomEmailExtension
    {
        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 = LogEventLevel.Error
        )
        {
            return loggerConfiguration.Email(
                connectionInfo,
                outputTemplate,
                restrictedToMinimumLevel
            );
        }
    
        public class CustomEmailConnectionInfo : EmailConnectionInfo
        {
            public CustomEmailConnectionInfo()
            {
                NetworkCredentials = new NetworkCredential();
            }
        }
    }