I've got a situation where some log events contain large property values (in this particular case, a large packet of XML). I want to use Serilog's ByIncludingOnly
functionality to use an additional sink for these large data events. Here's a sample of what I thought would work:
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;
}
However, when I run this code, all three messages go to the "big.txt" file. I would expect only the last item ("Big packet") to go to the file since it's the only event with a data
property that is over 10 characters.
I'm using Serilog 2.0.
答案 0 :(得分:3)
您的括号稍微偏离:
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();