Serilog Filter Expression: Filter expression doesn't work as expected with "... is not null"

135 views Asked by At

I have configured my serilog configuration in appsetting.json here is how I did :

 // Configuring Serilog


"Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.MSSqlServer", "Serilog.Expressions" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "connection string",
          "tableName": "Logs",
          "autoCreateSqlTable": true,
          "batchPostingLimit": 1,
          "period": "0:00:01",
          "columnOptions": {
            "AdditionalDataColumns": [
              {
                "ColumnName": "UserId",
                "DataType": "nvarchar",
                "DataLength": 256
              }
            ]
          }
        }
      },
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "connection string ",
          "tableName": "UserActivity",
          "autoCreateSqlTable": true,
          "batchPostingLimit": 1,
          "period": "0:00:01",
          "columnOptions": {
            "AdditionalDataColumns": [
              {
                "ColumnName": "UserId",
                "DataType": "nvarchar",
                "DataLength": 256
              }
            ]
          },
          "Properties": {
            "UserId": "UserId",
          },
          "Filter": [
            {
              "Name": "ByIncludingOnly",
              "Args": {
                "Expression": "UserId is not null"
              }
            }
          ]
        }
      }

    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
  },

Here as you see there is two tables, one Logs table and the other is UserActivity. Logs table can contains any type of logs and it should store all the logs but, I want to store only the logs with having UserId to the UserActivity table but it doesn't work as expected. all the logs with and without having UserId are logged in both tables.

can anyone help what is the problem?

0

There are 0 answers