Serilog: capture logs conditionally

Viewed 71

Is there a way to configure Serilog to only capture logs within a 'scope' when a condition is met ?

f.e.:

  • I want to capture the logs of a request with minimum level of 'Information' when an Exception occurred. Otherwise do not send the logs to a specific sink
  • I want to capture the logs of a request with minimum level of 'Information' when a log with level 'Error' or higher has been logged. Otherwise do not send the logs to a specific sink.

or:

  • Capture the logs to a specific sink when some flag is set in code at runtime:
using var scope = _logger.CreateScope(//enable or disable a specific sink for this scope)
_logger.LogInformation("...");
...
0 Answers
Related