Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hcholm opened this issue Jun 4, 2025 · 1 comment
Open

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

hcholm opened this issue Jun 4, 2025 · 1 comment

Comments

@hcholm
Copy link

hcholm commented Jun 4, 2025

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 hcholm added the bug Something isn't working label Jun 4, 2025
@nblumhardt nblumhardt added discussion and removed bug Something isn't working labels Jun 4, 2025
@nblumhardt
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants