Skip to content

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

@hcholm

Description

@hcholm

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions