'Serilog Filter Sink by Custom Property

I want to use Serilog.Expressions to filter my logging for a specific sink. In this instance, I only want to log to the Console Sink if my custom property MethodName is equal to "SomeOtherTask". Serilog is still logging to both Sinks for DoSomeWork and SomeOtherTask. Is there something wrong with my filter expression or how I have implemented my custom property MethodName or something else?

appsettings.json:

{
    "Serilog": {
        "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Expressions" ],
        "MinimumLevel": "Debug",
        "WriteTo": [
            {
                "Name": "Console",
                "Filter": [
                    {
                        "Name": "ByIncludingOnly",
                        "Args": {
                            "expression": "MethodName = 'SomeOtherTask'"
                        }
                    }
                ]
            },
            {
                "Name": "File",
                "Args": {
                    "path": "Logs/log.txt"
                }
            }
        ],
        "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
        "Properties": {
            "Application": "SerilogSplitLogTest"
        }
    }
}

Program.cs:

internal class Program
{
    static void Main()
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", true, true);
        
        var config = builder.Build();

        var logger = new LoggerConfiguration()
            .ReadFrom.Configuration(config)
            .CreateLogger();

        new Program().Run(logger);

        Console.ReadKey(true);
    }

    public void Run(ILogger logger)
    {
        DoSomeWork(logger);
        SomeOtherTask(logger);
    }

    public void DoSomeWork(ILogger logger)
    {
        logger.Information("This should log to File");
    }

    public void SomeOtherTask(ILogger logger)
    {
        using (LogContext.PushProperty("MethodName", nameof(SomeOtherTask)))
        {
            logger.Information("This should log to Console");
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source