Serilog 仅通过ByIncludingOnly筛选事件
我遇到了这样一种情况:一些日志事件包含大的属性值(在本例中是一个大的XML数据包)。我想通过只包含功能来使用Serilog的Serilog 仅通过ByIncludingOnly筛选事件,serilog,Serilog,我遇到了这样一种情况:一些日志事件包含大的属性值(在本例中是一个大的XML数据包)。我想通过只包含功能来使用Serilog的,为这些大型数据事件使用额外的接收器。以下是我认为有效的示例: private static void FilteredLogging() { Log.Logger = new LoggerConfiguration() .WriteTo.Console(new RawFormatter())
,为这些大型数据事件使用额外的接收器。以下是我认为有效的示例:
private static void FilteredLogging()
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(new RawFormatter())
.WriteTo.Logger(lc => lc.Filter.ByIncludingOnly(LargeDataPackets))
.WriteTo.File("big.txt")
.CreateLogger();
Log.Information("go");
Log.ForContext("data", "12345").Information("Small packet");
Log.ForContext("data", "1234567890987654321").Information("Big packet");
Log.CloseAndFlush();
}
private static bool LargeDataPackets(LogEvent le)
{
return le.Properties.ContainsKey("data") &&
le.Properties["data"].ToString().Length > 10;
}
但是,当我运行这段代码时,所有三条消息都会进入“big.txt”文件。我只希望最后一项(“大数据包”)进入该文件,因为它是唯一一个具有data
属性且超过10个字符的事件
我使用的是Serilog 2.0。您的括号在以下内容中有点偏离:
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(new RawFormatter())
.WriteTo.Logger(lc => lc.Filter.ByIncludingOnly(LargeDataPackets))
.WriteTo.File("big.txt")
.CreateLogger();
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(new RawFormatter())
.WriteTo.Logger(lc => lc.Filter.ByIncludingOnly(LargeDataPackets)
.WriteTo.File("big.txt"))
.CreateLogger();
应该是: