serilogappsettingsserilog-filter

Using Serilog in appsettings.json, my filter expression in the sublogger is not working


I'm using serilog to write logs to different files based upon namespace. Both files are being created, but nothing is being written to shopify.log. I have two namespaces that I want to include and both of them contain the string "Shopify". I can fully qualify them and pattern match, but that wasn't working either.

I'm using Serilog.Expressions 3.2.0

Any help is appreciated!

  "WriteTo": [
          { "Name": "Console" },
          {
            "Name": "File",
            "Args": {
              "path": "Logs/system.log"
            }
          },
          {
            "Name": "Logger",
            "Args": {
              "configureLogger": {
                "WriteTo": [
                  {
                    "Name": "File",
                    "Args": {
                      "path": "Logs/shopify.log"
                    }
                  }
                ],
                "Filter": [
                  {
                    "Name": "ByIncludingOnly",
                    "Args": {
                      "expression": "Contains(SourceContext, 'Shopify')"
                    }
                  }
                ]
              }
            }
          }
        ],

Solution

  • I'm still not sure why 'Contains' was not working, but I changed it to a more query type expression and that is working.

    "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "SourceContext like '%shopify%' ci"
                }
              }
            ]
    

    The 'ci' indicates that it is case-insensitive, so will match Shopify as well.