可以按短语过滤吗?

Filter by phrase possible?

对 serilog 有点陌生,但想做一些非常简单的事情,比如:

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .Filter.ByExcluding("Could not find file")
                .WriteTo.File("SimplePlayerApartment-.log", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 1)
                .CreateLogger();

我认为 Filter.ByExcluding 可以让我不记录其中包含某些 keywords/phrases 的行。相反,无论我能找到什么文档,它似乎都排除了基于命名空间、类 和各种花哨的东西,而我想要的只是过滤掉一个简单的字符串或 2.

有什么方法可以做我想做的事吗?正如上面的代码示例抛出一个大的运行时错误。

Filter.ByExcluding是一个谓词Func<LogEvent, bool>,所以你需要给它一个函数returns是否应该排除日志事件...

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Information()
    .Filter.ByExcluding(e => e.MessageTemplate.Text.Contains("Could not find file")) // <#<#<#<
    .WriteTo.File("SimplePlayerApartment-.log", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 1)
    .CreateLogger();

var fileName = "file.ext";

Log.Information("Hello");
Log.Information("Could not find file {fileName}", fileName); // Excluded from the log
Log.Information("Bye");

值得一提的是,上面的示例应用了基于消息模板的过滤器,消息模板(正如它所暗示的那样)是呈现之前的模板,因此 e.MessageTemplate.Text 将是 Could not find file {fileName} 而不是 Could not find file file.ext.