TraceSwitch 类

定义

提供多级开关,用于控制跟踪和调试输出,而无需重新编译代码。

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

示例

下面的代码示例创建一个新的 TraceSwitch ,并使用 开关确定是否打印错误消息。 开关是在类级别创建的。 MyMethod 如果 Level 属性设置为 TraceLevel.Error 或更高,则写入第一条错误消息。 但是, MyMethod 如果 Level 小于 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

注解

可以使用跟踪开关根据消息的重要性筛选出消息。 类 TraceSwitch 提供 TraceErrorTraceWarningTraceInfoTraceVerbose 属性来测试开关的级别。 属性 Level 获取或设置开关的 TraceLevel

可以在代码中创建 , TraceSwitch 并直接设置级别以检测代码的特定部分。

在仅.NET Framework应用中,还可以通过应用程序配置文件设置 的TraceSwitch级别,然后在应用程序中使用配置的TraceSwitch级别。 在应用程序配置文件中,可以添加或删除开关、设置交换机的值,或清除以前由应用程序设置的所有开关。 配置文件的格式应如以下示例所示:

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

此配置节定义了 , TraceSwitchDisplayName 设置为 mySwitchLevel 将 设置为 1,对应于枚举值 TraceLevel.Error

注意

还可以使用文本来指定开关的值。 例如, true 对于 BooleanSwitch,或表示枚举值的文本,例如 ErrorTraceSwitch。 行 <add name="mySwitch" value="Error" /> 等于 <add name="mySwitch" value="1" />

在应用程序中,可以通过创建 TraceSwitch 具有相同名称的 来使用配置的交换机级别,如以下示例所示:

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

在 .NET Core 和 .NET 5+ 应用中, Level 新开关的 默认为 TraceLevel.Off

在.NET Framework应用中,switch Level 属性默认为配置文件中指定的值。 TraceSwitch如果构造函数在配置文件中找不到初始开关设置,Level则新开关的 默认为 TraceLevel.Off

必须启用跟踪或调试才能使用开关。 以下语法特定于编译器。 如果使用 C# 或 Visual Basic 以外的编译器,请参阅编译器的文档。

  • 若要在 C# 中启用调试,请在编译代码时将 标志添加到 /d:DEBUG 编译器命令行,或将 添加到 #define DEBUG 文件的顶部。 在 Visual Basic 中,将 /d:DEBUG=True 标志添加到编译器命令行。

  • 若要在 C# 中启用跟踪,请在编译代码时将 标志添加到 /d:TRACE 编译器命令行,或将 添加到 #define TRACE 文件的顶部。 在 Visual Basic 中,将 /d:TRACE=True 标志添加到编译器命令行。

注意

单独使用 类时, TraceSwitch 不需要这些调试和跟踪编译器开关。 它们仅与 Trace 有条件编译的 或 Debug 方法结合使用时才需要。

有关检测应用程序的详细信息,请参阅 DebugTrace。 有关配置和使用跟踪开关的详细信息,请参阅 跟踪开关

注意

若要提高性能,可以在 TraceSwitch 类中创建成员 static

构造函数

TraceSwitch(String, String)

使用指定的显示名称和说明初始化 TraceSwitch 类的新实例。

TraceSwitch(String, String, String)

使用开关的指定显示名称、说明和默认值来初始化 TraceSwitch 类的新实例。

属性

Attributes

获取在应用程序配置文件中定义的自定义开关特性。

(继承自 Switch)
DefaultValue

获取构造函数中分配的默认值。

(继承自 Switch)
Description

获取开关说明。

(继承自 Switch)
DisplayName

获取用于标识该开关的名称。

(继承自 Switch)
Level

获取或设置跟踪级别,它将确定开关所允许的消息。

SwitchSetting

获取或设置此开关的当前设置。

(继承自 Switch)
TraceError

获取一个值,它指示开关是否允许错误处理消息。

TraceInfo

获取一个值,它指示开关是否允许信息性消息。

TraceVerbose

获取一个值,它指示开关是否允许所有消息。

TraceWarning

获取一个值,它指示开关是否允许警告消息。

Value

获取或设置开关的值。

(继承自 Switch)

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetSupportedAttributes()

获取开关支持的自定义特性。

(继承自 Switch)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
OnSwitchSettingChanged()

更新并更正此开关的级别。

OnValueChanged()

SwitchSetting 属性设置为 Value 属性的整数等效值。

OnValueChanged()

Value 属性更改时调用。

(继承自 Switch)
Refresh()

刷新跟踪配置数据。

(继承自 Switch)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅