Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the MinimumLevel in Serilog: is it possible to use a wildcard/regex for the namespace?

Setting the MinimumLevel in Serilog: is it possible to use wildcards/regex the namespace ?

Let's assume that I have my own namespaces with different casing, which need both to be logged on Information.

  • MyNameSpace
  • MYNameSpace

Can this be done via the configuration like this?

"SeriLog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Default": "Warning",
        "MyNameSpace|MYNameSpace": "Information"
      }
    }
  }

Or is this only possible by specifying both?

"SeriLog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Default": "Warning",
        "MyNameSpace": "Information",
        "MYNameSpace": "Information"
      }
    }
  }
like image 577
Stef Heyenrath Avatar asked Sep 01 '25 21:09

Stef Heyenrath


2 Answers

The minimum level override feature does not support wildcards/regex as of this writing. It only supports simple (case-sensitive) partial matching. You can see how it works in the source code.

Declaring the two different namespaces as your second example is the way to go.

like image 161
C. Augusto Proiete Avatar answered Sep 04 '25 12:09

C. Augusto Proiete


You can also exclude some log entries using Serilog.Expressions https://github.com/serilog/serilog/issues/1786#issuecomment-1487933383

I.e. set the minimum level for System.Net.Http.HttpClient to allow Information though, but use Filter.ByExcluding(...) and e.g. an expression to exclude SourceContext like 'System.Net.Http.HttpClient.%.LogicalHandler'.

like image 39
SerjG Avatar answered Sep 04 '25 14:09

SerjG