簡體   English   中英

Serilog 自定義接收器 - 使用 appsettings.json 設置最低級別不起作用

[英]Serilog custom sink - setting minimum level with appsettings.json not working

我正在嘗試編寫一個自定義的 Serilog 接收器,但我似乎無法讓“restrictedToMinimumLevel”參數工作。 我相信我的 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"
                }
            }
        ]
    }
}

定制水槽 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));
        }
    }
}

我想到了。 看來您必須在擴展方法中添加一個參數,如下所示:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM