[英]Serilog custom sink - setting minimum level with appsettings.json not working
I'm trying to write a custom Serilog sink, but I can't seem to get the "restrictedToMinimumLevel" arg to work.我正在尝试编写一个自定义的 Serilog 接收器,但我似乎无法让“restrictedToMinimumLevel”参数工作。 I believe my appsettings.json is correct, as the sink's emit method does run;
我相信我的 appsettings.json 是正确的,因为接收器的发射方法确实运行; however, the method runs for all log levels.
但是,该方法适用于所有日志级别。 What am I missing?
我错过了什么?
appsettings.json appsettings.json
{
"Serilog":
{
"Using":
[
"Serilog.Sinks.Console",
"CustomSerilogTest"
],
"MinimumLevel":
{
"Default": "Debug"
},
"WriteTo":
[
{
"Name": "Console",
"Args":
{
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u3}] {Message} {NewLine}{Exception}"
}
},
{
"Name": "MyCustomSink",
"Args":
{
"restrictedToMinimumLevel": "Error"
}
}
]
}
}
Custom Sink Class定制水槽 Class
namespace CustomSerilogTest
{
public class MyCustomSink : ILogEventSink
{
private readonly IFormatProvider _formatProvider;
public MyCustomSink(IFormatProvider formatProvider)
{
_formatProvider = formatProvider;
}
public void Emit(LogEvent logEvent)
{
//method fires, but is not limited to errors
var message = logEvent.RenderMessage(_formatProvider);
}
}
public static class SerilogExtensions
{
public static LoggerConfiguration MyCustomSink(
this LoggerSinkConfiguration loggerConfiguration,
IFormatProvider formatProvider = null)
{
return loggerConfiguration.Sink(new MyCustomSink(formatProvider));
}
}
}
I figured it out.我想到了。 It seems that you have to add a parameter in the extension method, like so:
看来您必须在扩展方法中添加一个参数,如下所示:
public static class SerilogExtensions
{
public static LoggerConfiguration MyCustomSink(
this LoggerSinkConfiguration loggerConfiguration,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null)
{
return loggerConfiguration.Sink(new MyCustomSink(formatProvider), restrictedToMinimumLevel: restrictedToMinimumLevel);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.