TraceSwitch TraceSwitch TraceSwitch TraceSwitch Class

定義

提供多層切換控制追蹤與偵錯的輸出,而不需要重新編譯程式碼。Provides a multilevel switch to control tracing and debug output without recompiling your code.

public ref class TraceSwitch : System::Diagnostics::Switch
public class TraceSwitch : System.Diagnostics.Switch
type TraceSwitch = class
    inherit Switch
Public Class TraceSwitch
Inherits Switch
繼承
TraceSwitchTraceSwitchTraceSwitchTraceSwitch

範例

下列程式碼範例會建立新TraceSwitch的, 並使用參數來決定是否要列印錯誤訊息。The following code example creates a new TraceSwitch and uses the switch to determine whether to print error messages. 參數是在類別層級建立。The switch is created at the class level. MyMethod如果Level屬性設定為TraceLevel.Error或更高版本, 則寫入第一個錯誤訊息。MyMethod writes the first error message if the Level property is set to TraceLevel.Error or higher. 不過, MyMethod Level如果小於TraceLevel.Verbose, 則不會寫入第二個錯誤訊息。However, MyMethod does not write the second error message if the Level is less than TraceLevel.Verbose.

   // Class-level declaration.
   /* Create a TraceSwitch to use in the entire application.*/
private:
   static TraceSwitch^ mySwitch = gcnew TraceSwitch( "General", "Entire Application" );

public:
   static void MyMethod()
   {
      // Write the message if the TraceSwitch level is set to Error or higher.
      if ( mySwitch->TraceError )
         Console::WriteLine( "My error message." );
      
      // Write the message if the TraceSwitch level is set to Verbose.
      if ( mySwitch->TraceVerbose )
         Console::WriteLine( "My second error message." );
   }

   static void main()
   {
      // Run the method that prints error messages based on the switch level.
      MyMethod();
   }
//Class-level declaration.
 /* Create a TraceSwitch to use in the entire application.*/
 static TraceSwitch mySwitch = new TraceSwitch("General", "Entire Application");
 
 static public void MyMethod() {
    // Write the message if the TraceSwitch level is set to Error or higher.
    if(mySwitch.TraceError)
       Console.WriteLine("My error message.");
 
    // Write the message if the TraceSwitch level is set to Verbose.
    if(mySwitch.TraceVerbose)
       Console.WriteLine("My second error message.");
 }
 
 public static void Main(string[] args) {
    // Run the method that prints error messages based on the switch level.
    MyMethod();
 }
 
' Class-level declaration.
' Create a TraceSwitch to use in the entire application. 
Private Shared mySwitch As New TraceSwitch("General", "Entire Application")    

Public Shared Sub MyMethod()
    ' Write the message if the TraceSwitch level is set to Error or higher.
    If mySwitch.TraceError Then
        Console.WriteLine("My error message.")
    End If 
    ' Write the message if the TraceSwitch level is set to Verbose.
    If mySwitch.TraceVerbose Then
        Console.WriteLine("My second error message.")
    End If
End Sub

Public Shared Sub Main()
    ' Run the method that prints error messages based on the switch level.
    MyMethod()
End Sub

備註

您可以使用追蹤參數, 根據其重要性來篩選訊息。You can use a trace switch to filter out messages based on their importance. TraceSwitch類別會TraceWarning提供、、TraceInfo和屬性TraceVerbose , 以測試參數的層級。 TraceErrorThe TraceSwitch class provides the TraceError, TraceWarning, TraceInfo, and TraceVerbose properties to test the level of the switch. 屬性會取得或設定TraceLevel參數的。 LevelThe Level property gets or sets the switch's TraceLevel.

您可以TraceSwitch透過應用程式佈建檔來設定的層級, 然後在您TraceSwitch的應用程式中使用設定的層級。You can set the level of a TraceSwitch through the application configuration file and then use the configured TraceSwitch level in your application. 或者, 您可以TraceSwitch在程式碼中建立, 並直接設定層級以檢測特定的程式碼區段。Alternately, you can create a TraceSwitch in your code and set the level directly to instrument a specific section of code.

若要設定TraceSwitch, 請編輯應用程式的設定檔。To configure a TraceSwitch, edit the configuration file for your application. 在此檔案中, 您可以新增或移除參數、設定參數的值, 或清除應用程式先前設定的所有參數。In 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>  

這個TraceSwitch DisplayName mySwitch設定區段TraceLevel.Error會定義, 並將設為, 並將設為1(對應至列舉值)。LevelThis configuration section defines a TraceSwitch with the DisplayName set to mySwitch, and the Level set to 1, which corresponds to the enumeration value TraceLevel.Error.

注意

在 .NET Framework 2.0 版中,您可以使用文字來指定參數的值。In the .NET Framework version 2.0, you can use text to specify the value for a switch. true例如, BooleanSwitch或代表TraceSwitch列舉值的文字, 例如的。ErrorFor example, true for a BooleanSwitch or the text representing an enumeration value, such as Error for a TraceSwitch. <add name="mySwitch" value="Error" /> 這一行相當於 <add name="mySwitch" value="1" />The line <add name="mySwitch" value="Error" /> is equivalent to <add name="mySwitch" value="1" />.

在您的應用程式中, 您可以藉由建立TraceSwitch具有相同名稱的來使用已設定的交換器層級, 如下列範例所示:In your application, you can use the configured switch level by creating a TraceSwitch with the same name, as shown in the following example:

private:
    static TraceSwitch^ appSwitch = gcnew TraceSwitch("mySwitch",
        "Switch in config file");

public:
    static void Main(array<String^>^ args)
    {
        //...
        Console::WriteLine("Trace switch {0} configured as {1}",
        appSwitch->DisplayName, appSwitch->Level.ToString());
        if (appSwitch->TraceError)
        {
            //...
        }
    }
private static TraceSwitch appSwitch = new TraceSwitch("mySwitch",
    "Switch in config file");

public static void Main(string[] args)
{
    //...
    Console.WriteLine("Trace switch {0} configured as {1}",
    appSwitch.DisplayName, appSwitch.Level.ToString());
    if (appSwitch.TraceError)
    {
        //...
    }
}
Private Shared appSwitch As new TraceSwitch("mySwitch", _
    "Switch in config file")

Public Shared Sub Main(args As String())
    '...
    Console.WriteLine("Trace switch {0} configured as {1}",
    appSwitch.DisplayName, appSwitch.Level.ToString())
    If appSwitch.TraceError = True  Then
        '...
    End If
End Sub

根據預設, switch Level屬性會使用設定檔中指定的值來設定。By default, the switch Level property is set using the value specified in the configuration file. 如果在TraceSwitch設定檔中找不到初始交換器設定Level , 則新交換器的會預設為TraceLevel.OffIf the TraceSwitch constructor cannot find initial switch settings in the configuration file, the Level of the new switch defaults to TraceLevel.Off.

您必須啟用追蹤或調試, 才能使用參數。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 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.

注意

在隔離中使用TraceSwitch類別時, 不需要這些 debug 和 trace 編譯器參數。These debug and trace compiler switches are not required when using the TraceSwitch 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.

注意

若要改善效能, 您可以TraceSwitchstatic類別中建立成員。To improve performance, you can make TraceSwitch members static in your class.

建構函式

TraceSwitch(String, String) TraceSwitch(String, String) TraceSwitch(String, String) TraceSwitch(String, String)

使用指定的顯示名稱與說明將 TraceSwitch 類別的新執行個體初始化。Initializes a new instance of the TraceSwitch class, using the specified display name and description.

TraceSwitch(String, String, String) TraceSwitch(String, String, String) TraceSwitch(String, String, String) TraceSwitch(String, String, String)

使用指定的參數顯示名稱、說明與預設值,將 TraceSwitch 類別的新執行個體初始化。Initializes a new instance of the TraceSwitch class, using the specified display name, description, and default value for the switch.

屬性

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)
Level Level Level Level

取得或設定決定參數允許之訊息的追蹤層級。Gets or sets the trace level that determines the messages the switch allows.

SwitchSetting SwitchSetting SwitchSetting SwitchSetting

取得或設定這個切換控制的目前設定。Gets or sets the current setting for this switch.

(Inherited from Switch)
TraceError TraceError TraceError TraceError

取得值,表示此參數是否允許錯誤處理訊息。Gets a value indicating whether the switch allows error-handling messages.

TraceInfo TraceInfo TraceInfo TraceInfo

取得值,表示此參數是否允許告知性訊息。Gets a value indicating whether the switch allows informational messages.

TraceVerbose TraceVerbose TraceVerbose TraceVerbose

取得值,表示此參數是否允許所有訊息。Gets a value indicating whether the switch allows all messages.

TraceWarning TraceWarning TraceWarning TraceWarning

取得值,表示此參數是否允許警告訊息。Gets a value indicating whether the switch allows warning messages.

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()

更新並修正此參數的層級。Updates and corrects the level for this switch.

OnValueChanged() OnValueChanged() OnValueChanged() OnValueChanged()

SwitchSetting 屬性設定成與 Value 屬性相等的整數。Sets the SwitchSetting property to the integer equivalent of the Value property.

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

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

另請參閱