Dynamic appsettings for Serilog.Sink.Mail

Viewed 365

I want to use Serilog.Sinks.Mail with config settings storred in an appsettings json file and the possibilty to dynamically change the subject and the mail receiver for every mail I send. I set {Message} for the mailSubject and with that I get the first words from my log message into my subject, but I want to predefine the subject for every mail.

Example: At event A send a mail to PersonA@something.com with subject "Event A". At event B send a mail to PersonB@something.com with subject "Event B" and so on.

Is there a way to do that with Serilog or should I use something else for the mails?

At the moment my appsettings for mails looks like this:

  {
    "Name": "Email",
    "Args": {
      "fromEmail": "fromEmail@something.com",
      "toEmail": "testmail@something.com",
      "mailServer": "testmailServer",
      "batchPostingLimit": 1,
      "mailSubject": "{Message}",
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine:1}{Exception:1}"
    }
1 Answers

I can't think of an easy way to do it. I think the best would be to configure in C# code instead of through the appsettings.json file.

You could for instance use Serilog.Sinks.Map (https://github.com/serilog/serilog-sinks-map) to configure a different sink based on the event that triggers it ...

You could still load common stuf from appsettings.json but you'd have to load those settings yourself.

Related