BooleanSwitch BooleanSwitch BooleanSwitch BooleanSwitch Class

定義

提供簡單的開啟/關閉切換,控制偵錯與追踨的輸出。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 DisplayName , 並將Enabled屬性設mySwitch為, 並將值true設定為。This 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. 將參數的值設定Enabled為 0, 將屬性設為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. 如果在BooleanSwitch設定檔中找不到初始交換器設定Enabled , 則新參數的屬性預設會設定為falseIf 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類別時, 不需要這些 debug 和 trace 編譯器參數。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.

如需檢測應用程式的詳細資訊, DebugTrace參閱和。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 的淺層複本 (Shallow Copy)。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)

適用於

另請參閱