serilog-expressions icon indicating copy to clipboard operation
serilog-expressions copied to clipboard

@p['x'] <> 2 is false if @p['x'] is undefined

Open hcholm opened this issue 6 months ago • 1 comments

It seems like @p['x'] <> 2 is evaluated to false if no context property x is given. I would expect an undefined value to be not equal to 2. It works if you check explicitly if x is defined.

Example code:

    var logger = new Serilog.LoggerConfiguration()
        .WriteTo.Logger(lc => lc
            .WriteTo.Console(new ExpressionTemplate("[ALL] {@m}\n")))

        // works
        .WriteTo.Logger(lc => lc
            .WriteTo.Console(new ExpressionTemplate("[x=2] {@m}\n"))
            .Filter.ByIncludingOnly("@p['x'] = 2"))

        // no output if x is undefined
        .WriteTo.Logger(lc => lc
            .WriteTo.Console(new ExpressionTemplate("[x<>2] {@m}\n"))
            .Filter.ByIncludingOnly("@p['x'] <> 2"))

        // works
        .WriteTo.Logger(lc => lc
            .WriteTo.Console(new ExpressionTemplate("[x<>2, def check] {@m}\n"))
            .Filter.ByIncludingOnly("not IsDefined(@p['x']) or @p['x'] <> 2"))
        .CreateLogger();

    logger.Information("Some info, no x");
    logger.ForContext("x", 2).Information("Some info, x is 2");
    logger.ForContext("x", 3).Information("Some info, x is 3");

Output:

[ALL] Some info, no x
[x<>2, def check] Some info, no x
[ALL] Some info, x is 2
[x=2] Some info, x is 2
[ALL] Some info, x is 3
[x<>2] Some info, x is 3
[x<>2, def check] Some info, x is 3

Expected (at least by me):

[ALL] Some info, no x
[x<>2] Some info, no x
[x<>2, def check] Some info, no x
[ALL] Some info, x is 2
[x=2] Some info, x is 2
[ALL] Some info, x is 3
[x<>2] Some info, x is 3
[x<>2, def check] Some info, x is 3

hcholm avatar Jun 04 '25 12:06 hcholm

Hi @hcholm, thanks for dropping us a line!

This is by design, "undefined" in the expression language has SQL-style "null semantics". It's not well-documented, I think in the short term adding a callout or similar is our best bet for heading off anyone encountering this unexpectedly in the future.

Is there a particular place you looked for info, or particular terms you searched for, when trying to figure out this one? Many thanks.

nblumhardt avatar Jun 04 '25 22:06 nblumhardt