BooleanSwitch 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供简单的打开/关闭开关来控制调试和跟踪输出。
public ref class BooleanSwitch : System::Diagnostics::Switchpublic class BooleanSwitch : System.Diagnostics.Switchtype BooleanSwitch = class
    inherit SwitchPublic Class BooleanSwitch
Inherits Switch- 继承
示例
以下示例创建 并使用 BooleanSwitch 开关确定是否打印错误消息。 在类级别创建开关。 方法 Main 将其位置传递给 MyMethod,这会输出错误消息以及发生错误的位置。
public ref class BooleanSwitchTest
{
private:
   /* Create a BooleanSwitch for data.*/
   static BooleanSwitch^ dataSwitch = gcnew BooleanSwitch( "Data","DataAccess module" );
public:
   static void MyMethod( String^ location )
   {
      
      //Insert code here to handle processing.
      if ( dataSwitch->Enabled )
            Console::WriteLine( "Error happened at {0}", location );
   }
};
int main()
{
   
   //Run the method which writes an error message specifying the location of the error.
   BooleanSwitchTest::MyMethod( "in main" );
}
// Class level declaration.
/* Create a BooleanSwitch for data.*/
static BooleanSwitch dataSwitch = new BooleanSwitch("Data", "DataAccess module");
static public void MyMethod(string location)
{
    //Insert code here to handle processing.
    if (dataSwitch.Enabled)
        Console.WriteLine("Error happened at " + location);
}
public static void Main(string[] args)
{
    //Run the method which writes an error message specifying the location of the error.
    MyMethod("in Main");
}
' Class level declaration.
' Create a BooleanSwitch for data. 
Private Shared dataSwitch As New BooleanSwitch("Data", "DataAccess module")
Public Shared Sub MyMethod(location As String)
    ' Insert code here to handle processing.
    If dataSwitch.Enabled Then
        Console.WriteLine(("Error happened at " + location))
    End If
End Sub
' Entry point which delegates to C-style main function.
Public Overloads Shared Sub Main()
    Main(System.Environment.GetCommandLineArgs())
End Sub
 
Overloads Public Shared Sub Main(args() As String)
    ' Run the method which writes an error message specifying the location of the error.
    MyMethod("in Main")
End Sub
注解
可以使用布尔跟踪开关根据消息的重要性启用或禁用消息。 Enabled使用 属性获取开关的当前值。
可以在代码中创建 , BooleanSwitch 并直接设置 Enabled 属性以检测代码的特定部分。
仅对于.NET Framework应用,还可以通过应用程序配置文件启用或禁用 ,BooleanSwitch然后在应用程序中使用配置BooleanSwitch的值。 若要配置 BooleanSwitch,请编辑与应用程序名称对应的配置文件。 在此文件中,可以添加或删除开关、设置开关的值,或清除以前由应用程序设置的所有开关。 配置文件的格式应如以下示例所示。
<configuration>  
  <system.diagnostics>  
    <switches>  
      <add name="mySwitch" value="1"/>  
    </switches>  
  </system.diagnostics>  
</configuration>  
此示例配置节定义了 , BooleanSwitch 其 DisplayName 属性设置为 mySwitch , Enabled 值设置为 true。 在 .NET Framework 应用程序中,可以通过创建BooleanSwitch具有相同名称的 来使用配置的开关值,如以下代码示例所示。
private:
    static BooleanSwitch^ boolSwitch = gcnew BooleanSwitch("mySwitch",
        "Switch in config file");
public:
    static void Main( )
    {
        //...
        Console::WriteLine("Boolean switch {0} configured as {1}",
            boolSwitch->DisplayName, ((Boolean^)boolSwitch->Enabled)->ToString());
        if (boolSwitch->Enabled)
        {
            //...
        }
    }
private static BooleanSwitch boolSwitch = new BooleanSwitch("mySwitch",
    "Switch in config file");
public static void Main()
{
    //...
    Console.WriteLine("Boolean switch {0} configured as {1}",
        boolSwitch.DisplayName, boolSwitch.Enabled.ToString());
    if (boolSwitch.Enabled)
    {
        //...
    }
}
Private Shared boolSwitch As new BooleanSwitch("mySwitch", _
    "Switch in config file")
Public Shared Sub Main( )
    '...
    Console.WriteLine("Boolean switch {0} configured as {1}",
        boolSwitch.DisplayName, boolSwitch.Enabled.ToString())
    If boolSwitch.Enabled = True Then
        '...
    End If
End Sub
对于 .NET Core 和 .NET 5+ 应用, Enabled 新开关的 属性默认设置为 false 。
对于.NET Framework应用,Enabled使用配置文件中指定的值设置 属性。 配置值为 0 的开关,将 Enabled 属性 false设置为 ;使用非零值配置开关,将 Enabled 属性设置为 true。 
              BooleanSwitch如果构造函数在配置文件中找不到初始交换机设置,Enabled则新交换机的 属性设置为 false。
必须启用跟踪或调试才能使用开关。 以下语法特定于编译器。 如果使用 C# 或 Visual Basic 以外的编译器,请参阅编译器的文档。
- 若要在 C# 中启用调试,请在编译代码时将 标志添加到 - /d:DEBUG编译器命令行,也可以将 添加到- #define DEBUG文件的顶部。 在 Visual Basic 中,将- /d:DEBUG=True标志添加到编译器命令行。
- 若要在 C# 中启用跟踪,请在编译代码时将 标志添加到 - /d:TRACE编译器命令行,或将 添加到- #define TRACE文件的顶部。 在 Visual Basic 中,将- /d:TRACE=True标志添加到编译器命令行。
注意
单独使用 类时, BooleanSwitch 不需要这些调试和跟踪编译器开关。 它们仅与 Trace 有条件编译的 或 Debug 方法结合使用时才需要。
有关检测应用程序的详细信息,请参阅 Debug 和 Trace。 有关配置和使用跟踪开关的详细信息,请参阅 跟踪开关。
注意
若要提高性能,可以在 BooleanSwitch 类中创建成员 static 。
构造函数
| BooleanSwitch(String, String) | 使用指定的显示名称和说明初始化 BooleanSwitch 类的新实例。 | 
| BooleanSwitch(String, String, String) | 使用指定的显示名称、描述和默认切换值初始化 BooleanSwitch 类的新实例。 | 
属性
| Attributes | 获取在应用程序配置文件中定义的自定义开关特性。(继承自 Switch) | 
| DefaultValue | 获取构造函数中分配的默认值。(继承自 Switch) | 
| Description | 获取开关说明。(继承自 Switch) | 
| DisplayName | 获取用于标识该开关的名称。(继承自 Switch) | 
| Enabled | 获取或设置一个指示开关已启用还是已禁用的值。 | 
| SwitchSetting | 获取或设置此开关的当前设置。(继承自 Switch) | 
| Value | 获取或设置开关的值。(继承自 Switch) | 
方法
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetSupportedAttributes() | 获取开关支持的自定义特性。(继承自 Switch) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| OnSwitchSettingChanged() | 当 SwitchSetting 属性更改时调用。(继承自 Switch) | 
| OnValueChanged() | 确定 Value 属性的新值是否可以被解析为一个布尔值。 | 
| OnValueChanged() | 当 Value 属性更改时调用。(继承自 Switch) | 
| Refresh() | 刷新跟踪配置数据。(继承自 Switch) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) |