Serilog 过滤器在子记录器上被忽略

问题描述 投票:0回答:1

我有一个 Serilog 配置对象,它已按要求工作,但我刚刚添加了一个额外的接收器(电子邮件)。

因此,我添加了子记录器,以允许我向每个接收器添加过滤器。

但是,我的电子邮件接收器没有观察过滤器,因此所有事件都被输出到它,而不仅仅是那些具有“EmailNotific”属性的事件。

n.b.我还尝试对事件的异常类型而不是属性使用过滤器,并且得到相同的结果。

任何人都可以提供任何建议/见解吗?

谢谢

LoggerConfiguration config = new();

config.WriteTo.Logger(x => x.Filter.ByExcluding(Matching.WithProperty("EmailNotifiable")))
      .WriteTo.Debug(restrictedToMinimumLevel: LogEventLevel.Verbose);

config.WriteTo.Logger(x => x.Filter.ByExcluding(Matching.WithProperty("EmailNotifiable")))
      .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Verbose);

string logFilePath = CreateLogFilePath(_loggerSettings.LogFileDirectory, _loggerSettings.LogFileName);

config.WriteTo.Logger(x => x.Filter.ByExcluding(Matching.WithProperty("EmailNotifiable")))
      .WriteTo.File(logFilePath,
                    rollingInterval: RollingInterval.Day,
                    restrictedToMinimumLevel: LogEventLevel.Information,
                    shared: true);

config.WriteTo.Logger(x => x.Filter.ByIncludingOnly(Matching.WithProperty("EmailNotifiable")))
      .WriteTo.Email(_connection);

---------------------
raising the log event:

_logger.ForContext("EmailNotifiable", true)
       .Warning(new EmailNotifiableException(body), message);

c# serilog serilog-filter
1个回答
0
投票

好吧,经过一番摆弄,我终于意识到我的错误 - 我错误地括起来 - 在定义其目标之前有效地关闭了子记录器。

正确的例子是:

config
    .WriteTo.Logger(c => c.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Verbose)
        .WriteTo.File($"{logFilePath}-VERBOSE-.log",
                      rollingInterval: RollingInterval.Day,
                      shared: true))

    .WriteTo.Logger(c => c.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Debug)
        .WriteTo.File($"{logFilePath}-DEBUG-.log",
                      rollingInterval: RollingInterval.Day,
                      shared: true))

    .WriteTo.Logger(c => c.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Information)
        .WriteTo.File($"{logFilePath}-INFO-.log",
                      rollingInterval: RollingInterval.Day,
                      shared: true))

    .WriteTo.Logger(c => c.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Warning)
        .WriteTo.File($"{logFilePath}-WARNING-.log",
                      rollingInterval: RollingInterval.Day,
                      shared: true))

    .WriteTo.Logger(c => c.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Error)
        .WriteTo.File($"{logFilePath}-ERROR-.log",
                      rollingInterval: RollingInterval.Day,
                      shared: true));
© www.soinside.com 2019 - 2024. All rights reserved.