2016-10-20 129 views
2

我有一些情况,其中一些日志事件包含大的属性值(在这种情况下,一个大型的XML包)。我想使用Serilog的ByIncludingOnly功能为这些大型数据事件使用额外的接收器。这里是什么,我认为会工作的一个样本:通过ByIncludingOnly过滤事件

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。

回答

2

你的括号是稍微偏离于:

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(); 
+0

卫生署!在男人 - 这是尴尬!谢谢尼古拉斯! – PatrickSteele