TraceSwitch 建構函式

定義

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

多載

TraceSwitch(String, String)

使用指定的顯示名稱與說明將 TraceSwitch 類別的新執行個體初始化。

TraceSwitch(String, String, String)

使用指定的參數顯示名稱、說明與預設值,將 TraceSwitch 類別的新執行個體初始化。

TraceSwitch(String, String)

來源:
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如果 屬性設定為 TraceLevel.Error 或更新版本,Level就會寫入第一個錯誤訊息。 不過,MyMethod如果 小於 TraceLevel.VerboseLevel則不會寫入第二個錯誤訊息。

   // Class-level declaration.
   /* Create a TraceSwitch to use in the entire application.*/
private:
   static TraceSwitch^ mySwitch = gcnew TraceSwitch( "General", "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("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 = gcnew TraceSwitch("mySwitch",
        "Switch in config file");

public:
    static void Main(array<String^>^ args)
    {
        //...
        Console::WriteLine("Trace switch {0} configured as {1}",
        appSwitch->DisplayName, appSwitch->Level.ToString());
        if (appSwitch->TraceError)
        {
            //...
        }
    }
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

注意

若要改善效能,您可以在類別中建立 TraceSwitch 成員 static

另請參閱

適用於

TraceSwitch(String, String, String)

來源:
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 是用來設定屬性的值 DisplayNamedescription 參數是用來設定屬性的值 Description ,而且 defaultSwitchValue 參數會儲存為欄位,並用來在第一次參考時初始化 Value 屬性。 如需詳細資訊和程式代碼範例,請參閱 建 TraceSwitch(String, String) 構函式。

適用於