共用方式為


TraceSwitch 建構函式

定義

初始化 TraceSwitch 類別的新執行個體。

多載

名稱 Description
TraceSwitch(String, String)

初始化該類別的新實例 TraceSwitch ,使用指定的顯示名稱與描述。

TraceSwitch(String, String, String)

初始化類別的新實例 TraceSwitch ,使用指定的顯示名稱、描述及交換器的預設值。

TraceSwitch(String, String)

來源:
TraceSwitch.cs
來源:
TraceSwitch.cs
來源:
TraceSwitch.cs
來源:
TraceSwitch.cs
來源:
TraceSwitch.cs

初始化該類別的新實例 TraceSwitch ,使用指定的顯示名稱與描述。

public:
 TraceSwitch(System::String ^ displayName, System::String ^ description);
public TraceSwitch(string displayName, string? description);
public TraceSwitch(string displayName, string description);
new System.Diagnostics.TraceSwitch : string * string -> System.Diagnostics.TraceSwitch
Public Sub New (displayName As String, description As String)

參數

displayName
String

名稱將顯示在使用者介面上。

description
String

交換器的描述。

範例

以下程式碼範例會建立一個新 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("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 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

備註

對於 .NET Framework 應用程式,要設定你的 TraceSwitch,請編輯與應用程式名稱對應的設定檔。 在這個檔案中,你可以新增一個開關並設定其值、移除一個開關,或清除應用程式先前設定的所有開關。 設定檔格式應如以下範例:

<configuration>
  <system.diagnostics>
    <switches>
      <add name="mySwitch" value="1" />
    </switches>
  </system.diagnostics>
</configuration>

你也可以用文字來指定開關的數值。 例如, true 對於 或 BooleanSwitch 表示列舉值的文本,例如 ErrorTraceSwitch。 該直線 <add name="mySwitch" value="Error" /> 等價於 <add name="mySwitch" value="1" />

在您的應用程式中,您可以透過建立同名的 參數 TraceSwitch 來使用已設定的交換器層級,如下範例所示:

private static TraceSwitch appSwitch = new TraceSwitch("mySwitch",
    "Switch in config file");

public static void Main(string[] args)
{
    //...
    Console.WriteLine("Trace switch {0} configured as {1}",
    appSwitch.DisplayName, appSwitch.Level.ToString());
    if (appSwitch.TraceError)
    {
        //...
    }
}
Private Shared appSwitch As new TraceSwitch("mySwitch", _
    "Switch in config file")

Public Shared Sub Main(args As String())
    '...
    Console.WriteLine("Trace switch {0} configured as {1}",
    appSwitch.DisplayName, appSwitch.Level.ToString())
    If appSwitch.TraceError = True  Then
        '...
    End If
End Sub

此構造子將 Level 新開關的性質設定為 TraceLevel.Off。 或者,對於 .NET Framework 應用程式,交換器設定則可從設定檔取得(若有的話)。

TraceSwitch類別提供 TraceErrorTraceWarningTraceInfoTraceVerbose性質以測試開關的 。Level 屬性取得 Level 或設定開關的 TraceLevel

備註

為了提升表現,你可以在你的班級中加入TraceSwitchstatic成員。

另請參閱

適用於

TraceSwitch(String, String, String)

來源:
TraceSwitch.cs
來源:
TraceSwitch.cs
來源:
TraceSwitch.cs
來源:
TraceSwitch.cs
來源:
TraceSwitch.cs

初始化類別的新實例 TraceSwitch ,使用指定的顯示名稱、描述及交換器的預設值。

public:
 TraceSwitch(System::String ^ displayName, System::String ^ description, System::String ^ defaultSwitchValue);
public TraceSwitch(string displayName, string? description, string defaultSwitchValue);
public TraceSwitch(string displayName, string description, string defaultSwitchValue);
new System.Diagnostics.TraceSwitch : string * string * string -> System.Diagnostics.TraceSwitch
Public Sub New (displayName As String, description As String, defaultSwitchValue As String)

參數

displayName
String

名稱將顯示在使用者介面上。

description
String

交換器的描述。

defaultSwitchValue
String

開關的預設值。

備註

參數 displayName 用於設定屬性的 DisplayName 值, description 參數用於設定屬性的 Description 值,參數 defaultSwitchValue 則儲存為欄位,並用於首次參照時初始化屬性 Value 。 請參閱 TraceSwitch(String, String) 建構器以獲得更多資訊及程式碼範例。

適用於