BooleanSwitch BooleanSwitch BooleanSwitch BooleanSwitch Class

定义

提供一个简单的 On/Off 开关,用于控制调试和跟踪输出。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
继承
BooleanSwitchBooleanSwitchBooleanSwitchBooleanSwitch

示例

下面的示例创建一个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. 方法将其位置传递到MyMethod, 这会打印错误消息以及错误发生的位置。 MainThe 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 mySwitch DisplayName 属性设置true为的, 并将值设置为。EnabledThis 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将开关Enabled配置为, 将属性设置为false; 将开关配置为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. 如果构造函数在配置文件中找不到初始开关设置Enabled , 则默认情况下, 新开关的属性将设置为。 false BooleanSwitchIf 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.

备注

在隔离中使用BooleanSwitch类时, 不需要这些调试和跟踪编译器开关。These 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.

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

备注

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

构造函数

BooleanSwitch(String, String) BooleanSwitch(String, String) BooleanSwitch(String, String) BooleanSwitch(String, String)

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

BooleanSwitch(String, String, String) BooleanSwitch(String, String, String) BooleanSwitch(String, String, String) BooleanSwitch(String, String, String)

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

属性

Attributes Attributes Attributes Attributes

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

(Inherited from Switch)
Description Description Description Description

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

(Inherited from Switch)
DisplayName DisplayName DisplayName DisplayName

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

(Inherited from Switch)
Enabled Enabled Enabled Enabled

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

SwitchSetting SwitchSetting SwitchSetting SwitchSetting

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

(Inherited from Switch)
Value Value Value Value

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

(Inherited from Switch)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetSupportedAttributes() GetSupportedAttributes() GetSupportedAttributes() GetSupportedAttributes()

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

(Inherited from Switch)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
OnSwitchSettingChanged() OnSwitchSettingChanged() OnSwitchSettingChanged() OnSwitchSettingChanged()

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

(Inherited from Switch)
OnValueChanged() OnValueChanged() OnValueChanged() OnValueChanged()

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

ToString() ToString() ToString() ToString()

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

(Inherited from Object)

适用于

另请参阅