Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Serilog 仅通过ByIncludingOnly筛选事件_Serilog - Fatal编程技术网

Serilog 仅通过ByIncludingOnly筛选事件

Serilog 仅通过ByIncludingOnly筛选事件,serilog,Serilog,我遇到了这样一种情况:一些日志事件包含大的属性值(在本例中是一个大的XML数据包)。我想通过只包含功能来使用Serilog的,为这些大型数据事件使用额外的接收器。以下是我认为有效的示例: private static void FilteredLogging() { Log.Logger = new LoggerConfiguration() .WriteTo.Console(new RawFormatter())

我遇到了这样一种情况:一些日志事件包含大的属性值(在本例中是一个大的XML数据包)。我想通过只包含功能来使用Serilog的
,为这些大型数据事件使用额外的接收器。以下是我认为有效的示例:

    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();
应该是: