次の方法で共有


TraceSwitch コンストラクター

定義

TraceSwitch クラスの新しいインスタンスを初期化します。

オーバーロード

名前 説明
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 以上に設定されている場合、最初のエラー メッセージを書き込みます。 ただし、LevelTraceLevel.Verbose未満の場合、MyMethodでは 2 番目のエラー メッセージは書き込まれません。

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

テキストを使用して、スイッチの値を指定することもできます。 たとえば、BooleanSwitchtrueや、列挙値を表すテキスト (TraceSwitchErrorなど)。 行 <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 クラスは、スイッチのLevelをテストするためのTraceErrorTraceWarningTraceInfo、およびTraceVerboseプロパティを提供します。 Level プロパティは、スイッチのTraceLevelを取得または設定します。

パフォーマンスを向上させるために、クラス TraceSwitch メンバーを static できます。

こちらもご覧ください

適用対象

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) コンストラクターを参照してください。

適用対象