BooleanSwitch 类

定义

提供简单的打开/关闭开关来控制调试和跟踪输出。

public ref class BooleanSwitch : System::Diagnostics::Switch
public class BooleanSwitch : System.Diagnostics.Switch
type BooleanSwitch = class
    inherit Switch
Public Class BooleanSwitch
Inherits Switch
继承
BooleanSwitch

示例

以下示例创建一个 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 类时,不需要这些调试和跟踪编译器开关。 它们仅与TraceDebug有条件编译的方法结合使用。

有关检测应用程序的详细信息,请参阅 DebugTrace。 有关配置和使用跟踪开关的详细信息,请参阅 跟踪开关

备注

为了提高性能,可以在 BooleanSwitch 课堂中创建成员 static

构造函数

BooleanSwitch(String, String)

使用指定的显示名称和说明初始化 BooleanSwitch 类的新实例。

BooleanSwitch(String, String, String)

使用指定的显示名称、描述和默认切换值初始化 BooleanSwitch 类的新实例。

属性

Attributes

获取在应用程序配置文件中定义的自定义开关特性。

(继承自 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)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅