TraceSwitch.Level 属性

定义

获取或设置跟踪级别,它将确定开关所允许的消息。Gets or sets the trace level that determines the messages the switch allows.

public:
 property System::Diagnostics::TraceLevel Level { System::Diagnostics::TraceLevel get(); void set(System::Diagnostics::TraceLevel value); };
public System.Diagnostics.TraceLevel Level { get; set; }
member this.Level : System.Diagnostics.TraceLevel with get, set
Public Property Level As TraceLevel

属性值

TraceLevel值之一, 指定开关允许的消息级别。One of the TraceLevel values that specifies the level of messages that are allowed by the switch.

异常

Level 设置为非 TraceLevel 值。Level is set to a value that is not one of the TraceLevel values.

示例

下面的代码示例创建一个新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( "mySwitch","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("mySwitch", "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("mySwitch", "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, 请编辑与应用程序名称对应的配置文件。To set the level of your TraceSwitch, edit the configuration file that corresponds to the name of your application. 在此文件中, 可以添加开关并设置其值、删除开关或清除以前由应用程序设置的所有开关。In this file, you can add a switch and set its value, remove a switch, 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="0" />  
      <add name="myNewSwitch" value="3" />  
      <remove name="mySwitch" />  
      <clear/>  
    </switches>  
  </system.diagnostics>  
</configuration>  

备注

在 .NET Framework 2.0 版中,你可以使用文本指定开关值。In the .NET Framework version 2.0, you can use text to specify the value for a switch. 例如, true Error对于或表示枚举值TraceSwitch的文本 (例如)。 BooleanSwitchFor 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" />.

如果构造函数在配置文件中找不到初始开关设置Level , 则新开关的属性将设置为TraceLevel.OffTraceSwitchWhen the TraceSwitch constructor cannot find initial switch settings in the configuration file, the Level property of the new switch is set to TraceLevel.Off.

设置此属性将更新TraceErrorTraceWarningTraceInfoTraceVerbose属性以反映新的值。Setting this property updates the TraceError, TraceWarning, TraceInfo, and TraceVerbose properties to reflect the new value.

安全性

SecurityPermission
用于处理非托管代码。for operating with unmanaged code. 关联的枚举:UnmanagedCode安全操作: LinkDemandAssociated enumeration: UnmanagedCode Security action: LinkDemand.

适用于

另请参阅