简体   繁体   English

Serilog.Sink.Mail 的动态应用设置

[英]Dynamic appsettings for Serilog.Sink.Mail

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.我想将 Serilog.Sinks.Mail 与存储在 appsettings json 文件中的配置设置一起使用,并且可以动态更改我发送的每封邮件的主题和邮件接收器。 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.我为 mailSubject 设置了 {Message},然后我将日志消息中的第一个单词输入到我的主题中,但我想为每封邮件预先定义主题。

Example: At event A send a mail to PersonA@something.com with subject "Event A".示例:在事件 A 中,向 PersonA@something.com 发送主题为“事件 A”的邮件。 At event B send a mail to PersonB@something.com with subject "Event B" and so on.在事件 B 中,向 PersonB@something.com 发送一封邮件,主题为“事件 B”,依此类推。

Is there a way to do that with Serilog or should I use something else for the mails?有没有办法用 Serilog 做到这一点,还是我应该使用其他东西来处理邮件?

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}"
    }

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.我认为最好的方法是在 C# 代码中进行配置,而不是通过appsettings.json文件进行配置。

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 ...例如,您可以使用Serilog.Sinks.Map ( https://github.com/serilog/serilog-sinks-map ) 根据触发它的事件配置不同的接收器......

You could still load common stuf from appsettings.json but you'd have to load those settings yourself.您仍然可以从appsettings.json加载常见的 stuf,但您必须自己加载这些设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM