File: src\libraries\Common\src\System\AppContextSwitchHelper.cs
Web Access
Project: src\src\libraries\System.Net.Security\src\System.Net.Security.csproj (System.Net.Security)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System.Globalization;
 
namespace System
{
    internal static class AppContextSwitchHelper
    {
        internal static bool GetBooleanConfig(string switchName, bool defaultValue = false) =>
            AppContext.TryGetSwitch(switchName, out bool value) ? value : defaultValue;
 
        internal static bool GetBooleanConfig(string switchName, string envVariable, bool defaultValue = false)
        {
            if (AppContext.TryGetSwitch(switchName, out bool value))
            {
                return value;
            }
 
            if (Environment.GetEnvironmentVariable(envVariable) is string str)
            {
                if (str == "1" || string.Equals(str, bool.TrueString, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
                if (str == "0" || string.Equals(str, bool.FalseString, StringComparison.OrdinalIgnoreCase))
                {
                    return false;
                }
            }
 
            return defaultValue;
        }
    }
}