TraceSwitch.Level 属性

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

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

语法

声明
Public Property Level As TraceLevel
用法
Dim instance As TraceSwitch
Dim value As TraceLevel

value = instance.Level

instance.Level = value
public TraceLevel Level { get; set; }
public:
property TraceLevel Level {
    TraceLevel get ();
    void set (TraceLevel value);
}
/** @property */
public TraceLevel get_Level ()

/** @property */
public void set_Level (TraceLevel value)
public function get Level () : TraceLevel

public function set Level (value : TraceLevel)

属性值

TraceLevel 值之一,它指定开关所允许的消息的级别。

异常

异常类型 条件

ArgumentException

Level 设置为非 TraceLevel 值。

备注

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

<configuration>
  <system.diagnostics>
    <switches>
      <add name="mySwitch" value="0" />
      <add name="myNewSwitch" value="3" />
      <remove name="mySwitch" />
      <clear/>
    </switches>
  </system.diagnostics>
</configuration>

TraceSwitch 构造函数无法在配置文件中找到初始开关设置时,新开关的 Level 属性将设置为 TraceLevel.Off

设置此属性可更新 TraceErrorTraceWarningTraceInfoTraceVerbose 属性以反映新值。

示例

下面的代码示例创建一个新的 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

平台

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 类
TraceSwitch 成员
System.Diagnostics 命名空间
TraceSwitch 类
TraceLevel 枚举
Switch 类
Debug 类
Trace 类