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定義 屬性設定為 mySwitchDisplayName ,並將 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為 ;以非零值設定參數,Enabled將 屬性設定為 truefalse 如果建 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 方法搭配使用。

如需偵測應用程式的詳細資訊,請參閱 DebugTrace。 如需設定和使用追蹤參數的詳細資訊,請參閱 追蹤參數

注意

若要改善效能,您可以在類別中建立 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)

適用於

另請參閱