17

Is there a way to get the minimum log level from Serilog's current configuration in a controller or middleware?

andre_ss6
  • 1,195
  • 1
  • 13
  • 34
  • Do you want to get minimum log level from `appsettings.json` or `Serilog.Log.Logger` object? For previous, you could bind the configuration to `IOptions`, refer [Configure Column Options for Serilog Sinks MsSqlServer in AppSettings.json ](https://stackoverflow.com/questions/48690921/configure-column-options-for-serilog-sinks-mssqlserver-in-appsettings-json/48701159#48701159) – Edward Mar 12 '18 at 06:30

1 Answers1

26

Though it is slightly indirect, you can use IsEnabled to check this level by level:

Log.IsEnabled(LogEventLevel.Debug)

This is usually enough for these kinds of scenarios. If you really need the precise minimum level, you can try the various LogEventLevel members individually to figure it out. IsEnabled() is very fast, so checking a few levels this way won't show up on your performance radar.

Keep in mind that the minimum level can still be overridden per source context (i.e. with MinimumLevel.Override()).

jpmc26
  • 28,463
  • 14
  • 94
  • 146
Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • 8
    Maybe `Enum.GetValues(typeof(LogEventLevel)).Cast().Where(Log.IsEnabled).Min()`? Although I guess a custom loop could short circuit. – jpmc26 Mar 12 '18 at 04:47