通过


TraceSwitch.Level 属性

定义

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

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 开关允许的消息级别的值之一。

例外

Level 设置为不是值 TraceLevel 之一的值。

示例

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

//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

注解

对于 .NET Framework 应用,若要设置级别 TraceSwitch,请编辑对应于应用程序名称的配置文件。 在此文件中,可以添加开关并设置其值、删除开关或清除应用程序之前设置的所有开关。 配置文件的格式应如以下示例所示:

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

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

Level 属性的默认值为 TraceLevel.Off。 或者,对于 .NET Framework 应用,从配置文件获取级别(如果可用)。

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

适用于

另请参阅