.net Core和Serilog电子邮件接收器-json配置 [英] .net Core and Serilog Email sink - json config

查看:733
本文介绍了.net Core和Serilog电子邮件接收器-json配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.net Core 2.0和Serilog电子邮件接收器.我在使用appsettings.json配置电子邮件接收器时遇到问题. program.cs中的相同配置有效,而appsetting.json中的相同无效.

I'm using .net Core 2.0 and Serilog Email sink. I have problem to configure email sink with appsettings.json. The same configuration from program.cs is working while one from appsetting.json isn't.

推荐答案

设置系统(ReadFrom.Configuration())实际上仅尝试尝试调用方法和扩展方法,它可以发现并传递配置文件中提供的参数.

The settings system (ReadFrom.Configuration()) really only does try to call methods and extension methods that it can discover and pass arguments provided from the configuration file.

不幸的是,它暂时仅支持 basic 类型(可转换为string和/或从string转换为更多特定情况),因此,无法提供类型为EmailConnectionInfo的参数.

Unfortunately, it only supports basic types for the time being (convertible to/from string and a few more specific cases) and therefore, parameters of type EmailConnectionInfo cannot be provided.

但是,作为一种解决方法,如果只需要传递一些参数,则可以创建自己的扩展方法,该方法接受所需的参数并从配置系统中调用它.

As a workaround, though, if you only need to pass in a few parameters, you can create your own extension method that accepts the parameters that you need and call it from the configuration system.

对于您而言,您需要执行以下操作:

In your case, you would need to do the following :

首先,定义扩展方法 EmailCustom(...),可以将其插入WriteTo(类型为Serilog.Configuration.LoggerSinkConfiguration)并返回LoggerConfiguration.

First, define an extension method EmailCustom(...) that can be plugged on WriteTo (which is of type Serilog.Configuration.LoggerSinkConfiguration) and returns a LoggerConfiguration.

这看起来像(未经测试,没有使用等:P):

This would look something like (not tested, no usings etc :P) :

namespace Serilog{
    public static class MyCustomExtensions
    {
        public static LoggerConfiguration EmailCustom(this LoggerSinkConfiguration sinkConfiguration, string param1, int param2, LogEventLevel restrictedToMinimumLevel){
            // the actual call to configure the Email sink, passing in complex parameters
            return sinkConfiguration.Email(... ... , restrictedToMinimumLevel , EmailConnectionInfo(){
            Foo = "bar",
            Baz = param1,
            Qux = param2,
            }
            );
        }
    }
}

从那时起,您应该能够编写如下的C#代码:

From that point on, you should be able to write C# code like :

new LoggerConfiguration()
    .WriteTo.EmailCustom(param1: "my param1", param2: 42)
   // ...
    .CreateLogger();

一旦完成工作,就可以在 json 中定义该方法调用,这要归功于 Serilog.Settings.Configuration 在那种情况下,看起来就像

Once you have that working, you can actually define that method call in json thanks to Serilog.Settings.Configuration in that case, that would look like

{
"Serilog": {
    "Using" : ["TheNameOfTheAssemblyThatContainsEmailCustom"],
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "EmailCustom",
        "Args": {
          "param1": "my param1",
          "param2": 42,
          "restrictedToMinimumLevel": "Verbose"
        }
      }]
    }
}

此策略也可以应用于Sinklog的其他接收器和其他配置部分.

This strategy can be applied for other sinks and other configuration parts of Serilog as well.

您可以在这里找到有关配置系统的更多信息:

You can find a bit more about the configuration system here :

  • the project's README Serilog.Settings.Configuration
  • examples of what can be done through configuration (shameless plug :p)

这篇关于.net Core和Serilog电子邮件接收器-json配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆