Skip to content

Instantly share code, notes, and snippets.

@bluewalk
Last active April 19, 2021 05:26
Show Gist options
  • Save bluewalk/03ee8d0b2fb0864441a2d15adde2439e to your computer and use it in GitHub Desktop.
Save bluewalk/03ee8d0b2fb0864441a2d15adde2439e to your computer and use it in GitHub Desktop.
Serilog-EnvironmentVariableLoggingLevelSwitch

Usage

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.ControlledBy(new EnvironmentVariableLoggingLevelSwitch("LOG_LEVEL"))
    .CreateLogger();
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