BooleanSwitch 类

定义

提供简单的打开/关闭开关来控制调试和跟踪输出。Provides a simple on/off switch that controls debugging and tracing output.

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 ,并使用开关来确定是否打印错误消息。The following example creates a BooleanSwitch and uses the switch to determine whether to print an error message. 在类级别创建开关。You create the switch at the class level. Main方法将其位置传递到 MyMethod ,这会打印错误消息以及错误发生的位置。The Main method passes its location to MyMethod, which prints an error message and where the error occurred.

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

注解

可以根据消息的重要性,使用布尔跟踪开关来启用或禁用消息。You can use a Boolean trace switch to enable or disable messages based on their importance. 使用 Enabled 属性可获取开关的当前值。Use the Enabled property to get the current value of the switch.

你可以 BooleanSwitch 通过应用程序配置文件启用或禁用,然后 BooleanSwitch 在你的应用程序中使用配置的值。You can enable or disable a BooleanSwitch through the application configuration file and then use the configured BooleanSwitch value in your application. 或者,您可以 BooleanSwitch 在代码中创建,并直接设置 Enabled 属性以检测代码的特定部分。Alternately, you can create a BooleanSwitch in your code and set the Enabled property directly to instrument a specific section of code.

若要配置 BooleanSwitch ,请编辑与应用程序名称对应的配置文件。To configure a BooleanSwitch, edit the configuration file that corresponds to the name of your application. 在此文件中,可以添加或删除开关、设置开关的值,或者清除以前由应用程序设置的所有开关。Within this file, you can add or remove a switch, set a switch's value, or clear all the switches previously set by the application. 配置文件的格式应与下面的示例类似。The configuration file should be formatted like the following example.

<configuration>  
  <system.diagnostics>  
    <switches>  
      <add name="mySwitch" value="1"/>  
    </switches>  
  </system.diagnostics>  
</configuration>  

此示例配置节定义 BooleanSwitch DisplayName 属性设置为的 mySwitch ,并将 Enabled 值设置为 trueThis example configuration section defines a BooleanSwitch with the DisplayName property set to mySwitch and the Enabled value set to true. 在应用程序中,可以通过创建一个具有相同名称的来使用已配置的开关值 BooleanSwitch ,如下面的代码示例中所示。Within your application, you can use the configured switch value by creating a BooleanSwitch with the same name, as shown in the following code example.

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

默认情况下, Enabled 使用配置文件中指定的值设置属性。By default, the Enabled property is set using the value specified in the configuration file. 使用值0将开关配置为,将属性设置 Enabledfalse ; 将开关配置为非零值,以将 Enabled 属性设置为 trueConfigure the switch with a value of 0 to set the Enabled property to false; configure the switch with a nonzero value to set the Enabled property to true. 如果 BooleanSwitch 构造函数在配置文件中找不到初始开关设置,则 Enabled false 默认情况下,新开关的属性将设置为。If the BooleanSwitch constructor cannot find initial switch settings in the configuration file, the Enabled property of the new switch is set to false by default.

您必须启用跟踪或调试才能使用交换机。You must enable tracing or debugging to use a switch. 以下语法是编译器特定的。The following syntax is compiler specific. 如果使用 c # 或 Visual Basic 以外的编译器,请参阅编译器的文档。If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler.

  • 若要在 c # 中启用调试,请在 /d:DEBUG 编译代码时将标志添加到编译器命令行,或者将添加 #define DEBUG 到文件的顶部。To enable debugging in C#, add the /d:DEBUG flag to the compiler command line when you compile your code, or you can add #define DEBUG to the top of your file. 在 Visual Basic 中,将 /d:DEBUG=True 标志添加到编译器命令行。In Visual Basic, add the /d:DEBUG=True flag to the compiler command line.

  • 若要在 c # 中启用跟踪,请在 /d:TRACE 编译代码时将标志添加到编译器命令行,或将添加 #define TRACE 到文件的顶部。To enable tracing in C#, add the /d:TRACE flag to the compiler command line when you compile your code, or add #define TRACE to the top of your file. 在 Visual Basic 中,将 /d:TRACE=True 标志添加到编译器命令行。In Visual Basic, add the /d:TRACE=True flag to the compiler command line.

备注

在隔离中使用类时,不需要这些调试和跟踪编译器开关 BooleanSwitchThese debug and trace compiler switches are not required when using the BooleanSwitch class in isolation. 只需结合 Trace Debug 有条件地编译的或方法。They are only required in conjunction with Trace or Debug methods that are conditionally compiled.

有关检测应用程序的详细信息,请参阅 DebugTraceFor more information on instrumenting your application, see Debug and Trace. 有关配置和使用跟踪开关的详细信息,请参阅 跟踪开关For more information about configuring and using trace switches, see Trace Switches.

备注

若要提高性能,可以 BooleanSwitch static 在类中创建成员。To improve performance, you can make BooleanSwitch members static in your class.

构造函数

BooleanSwitch(String, String)

使用指定的显示名称和说明初始化 BooleanSwitch 类的新实例。Initializes a new instance of the BooleanSwitch class with the specified display name and description.

BooleanSwitch(String, String, String)

使用指定的显示名称、描述和默认切换值初始化 BooleanSwitch 类的新实例。Initializes a new instance of the BooleanSwitch class with the specified display name, description, and default switch value.

属性

Attributes

获取在应用程序配置文件中定义的自定义开关特性。Gets the custom switch attributes defined in the application configuration file.

(继承自 Switch)
Description

获取开关说明。Gets a description of the switch.

(继承自 Switch)
DisplayName

获取用于标识该开关的名称。Gets a name used to identify the switch.

(继承自 Switch)
Enabled

获取或设置一个指示开关已启用还是已禁用的值。Gets or sets a value indicating whether the switch is enabled or disabled.

SwitchSetting

获取或设置此开关的当前设置。Gets or sets the current setting for this switch.

(继承自 Switch)
Value

获取或设置开关的值。Gets or sets the value of the switch.

(继承自 Switch)

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetSupportedAttributes()

获取开关支持的自定义特性。Gets the custom attributes supported by the switch.

(继承自 Switch)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OnSwitchSettingChanged()

SwitchSetting 属性更改时调用。Invoked when the SwitchSetting property is changed.

(继承自 Switch)
OnValueChanged()

确定 Value 属性的新值是否可以被解析为一个布尔值。Determines whether the new value of the Value property can be parsed as a Boolean value.

OnValueChanged()

Value 属性更改时调用。Invoked when the Value property is changed.

(继承自 Switch)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅