TraceSwitch 类

提供多级开关控制跟踪和调试输出而不必重新编译代码。

**命名空间:**System.Diagnostics
**程序集:**System(在 system.dll 中)

语法

声明
Public Class TraceSwitch
    Inherits Switch
用法
Dim instance As TraceSwitch
public class TraceSwitch : Switch
public ref class TraceSwitch : public Switch
public class TraceSwitch extends Switch
public class TraceSwitch extends Switch

备注

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

可以通过应用程序配置文件来设置 TraceSwitch 的级别,然后在应用程序中使用此已配置的 TraceSwitch 级别。或者,可以在代码中创建 TraceSwitch 并直接设置其级别,以检测特定部分的代码。

若要配置 TraceSwitch,请编辑与应用程序名对应的配置文件。在该文件中,可以添加或移除开关、设置开关的值或清除以前由应用程序设置的所有开关。应像下面的示例这样对配置文件进行格式化:

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

此配置节通过将 DisplayName 设置为 mySwitch,并将 Level 设置为 1(对应于枚举值 TraceLevel.Error)来定义 TraceSwitch。在应用程序中,通过创建名称相同的 TraceSwitch,可以使用已配置的开关级别,如下面的示例所示:

Private Shared appSwitch As New TraceSwitch("mySwitch", _
"Switch in config file")
Public Shared Sub Main(ByVal CmdArgs() As String)
'...
    Console.WriteLine("Trace switch {0} configured as {1}", _
    appSwitch.DisplayName, appSwitch.Level.ToString())
    If appSwitch.TraceError Then
        '...
    End If
End Sub
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)
    {
        //...
    }
}

默认情况下,开关 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 类时,不需要使用这些调试和跟踪编译器开关。只有在与按条件编译的 TraceDebug 方法一起使用时,这些开关才是必需的。

有关配置应用程序的更多信息,请参见 DebugTrace。有关配置和使用跟踪开关的更多信息,请参见跟踪开关

提示

若要提高性能,可以在您的类中将 TraceSwitch 成员设置为 static

主题 位置
如何:创建和初始化跟踪开关 .NET Framework:调试
如何:配置跟踪开关 .NET Framework:调试
如何:配置跟踪开关 .NET Framework:调试
如何:创建和初始化跟踪开关 .NET Framework:调试

示例

下面的代码示例创建一个新的 TraceSwitch 并使用此开关确定是否打印错误信息。此开关在类级别创建。如果 Level 属性设置为 TraceLevel.Error 或更高级别,MyMethod 会写入第一条错误信息。但是,当 Level 小于 TraceLevel.Verbose 时,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
//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:
   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.
 */
private static TraceSwitch mySwitch =
    new TraceSwitch("General", "Entire Application");

public static void MyMethod()
{
    //Write the message if the TraceSwitch level is set to Error or higher.
    if (mySwitch.get_TraceError()) {
        Console.WriteLine("My error message.");
    }

    // Write the message if the TraceSwitch level is set to Verbose.
    if (mySwitch.get_TraceVerbose()) {
        Console.WriteLine("My second error message.");
    }
} //MyMethod

public static void main(String[] args)
{
    // Run the method that prints error messages based on the switch level.
    MyMethod();
} //main

继承层次结构

System.Object
   System.Diagnostics.Switch
    System.Diagnostics.TraceSwitch

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

TraceSwitch 成员
System.Diagnostics 命名空间
Switch 类
BooleanSwitch 类
TraceLevel 枚举
Debug 类
Trace 类