.net Core 和 Serilog 电子邮件接收器 - json 配置 [英] .net Core and Serilog Email sink - json config
问题描述
我正在使用 .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.
不幸的是,它暂时只支持基本类型(可转换为/从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"
}
}]
}
}
该策略也适用于 Serilog 的其他接收器和其他配置部分.
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屋!