Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(new EnvironmentVariableLoggingLevelSwitch("LOG_LEVEL"))
.CreateLogger();
Last active
April 19, 2021 05:26
-
-
Save bluewalk/03ee8d0b2fb0864441a2d15adde2439e to your computer and use it in GitHub Desktop.
Serilog-EnvironmentVariableLoggingLevelSwitch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EnvironmentVariableLoggingLevelSwitch : LoggingLevelSwitch | |
{ | |
public EnvironmentVariableLoggingLevelSwitch(string environmentVariable, | |
LogEventLevel defaultLevel = LogEventLevel.Information) | |
{ | |
MinimumLevel = | |
Enum.TryParse<LogEventLevel>(Environment.GetEnvironmentVariable(environmentVariable), true, | |
out var level) | |
? level | |
: defaultLevel; | |
#if DEBUG | |
MinimumLevel = LogEventLevel.Debug; | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment