Is there a way to get the minimum log level from Serilog's current configuration in a controller or middleware?
Asked
Active
Viewed 6,175 times
17
-
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 Answers
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
-
8Maybe `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