TraceSwitch.Level 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定追蹤電平,決定交換器允許的訊息。
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 如果屬性 Level 設定為或 TraceLevel.Error 更高,則會寫出第一個錯誤訊息。 然而,MyMethod若 小LevelTraceLevel.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 表示列舉值的文本,例如 ErrorTraceSwitch。 該直線 <add name="mySwitch" value="Error" /> 等價於 <add name="mySwitch" value="1" />。
屬性的 Level 預設值為 TraceLevel.Off。 或者,對於 .NET Framework 應用程式,等級則可從設定檔取得(若有的話)。
設定此屬性會更新 TraceError、 TraceWarning、 TraceInfo和 TraceVerbose 屬性以反映新值。