Serilog | How to control logging level based upon a custom filter

410 Views Asked by At

I would like to be able to dynamically control the logging level for Serilog based upon a custom query. At minimum, I would like to be able to filter log messages by the start of their SourceContext (ie, the namespace/full class name) - this could be optimized with a compiled regex statement/state machine to handle the matching.

Ideally, in addition to the above, I could create SourceContext filters that are applicable to only certain users - such as via a UserId field on the event - that would take priority over filters that don't have a UserId specified.

I would like to be able to dynamically modify the set of such filters via an Admin website, allowing myself or other admins to increase/decrease the verbosity level of specific parts of the code and for specific users as the need arises.

I was wondering if anyone here knows the best way to go about implementing such a feature with Serilog? I've googled this, but I haven't found any filtering capabilities out of the box for Serilog that look like they'd be up to this task. In which case, I might go about it by means of an intermediate, in-process Sink.

1

There are 1 best solutions below

0
On BEST ANSWER

I found an answer to a related but distinct issue that looks like a good solution: How to change the LogLevel of specific log events in Serilog?_

It looks like Serilog does provide a mechanism for wrapping sinks in intermediate sinks, and it shouldn't be too difficult to apply the filtration logic I'm seeking by doing so.