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如果 小於 TraceLevel.VerboseLevel則不會寫入第二個錯誤訊息。

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

備註

針對 .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 列舉值的 或 文字,例如 ErrorTraceSwitch<add name="mySwitch" value="Error" /> 這一行相當於 <add name="mySwitch" value="1" />

屬性的 Level 預設值為 TraceLevel.Off。 或者,對於 .NET Framework 應用程式,如果可用,則會從組態檔取得層級。

設定此屬性會 TraceError更新、 TraceWarningTraceInfoTraceVerbose 屬性,以反映新的值。

適用於

另請參閱