BooleanSwitchとは? わかりやすく解説

BooleanSwitch クラス

デバッグおよびトレースの出力を制御する単純なオン/オフ スイッチを提供します。

名前空間: System.Diagnostics
アセンブリ: System (system.dll 内)
構文構文

Public Class BooleanSwitch
    Inherits Switch
Dim instance As BooleanSwitch
public class BooleanSwitch : Switch
public ref class BooleanSwitch : public
 Switch
public class BooleanSwitch extends Switch
public class BooleanSwitch extends
 Switch
解説解説

ブール型トレースを使用し、メッセージの重要性に基づいてメッセージの有効と無効を切り替えることができます。Enabled プロパティを使用して、スイッチの現在の値を取得します。

アプリケーション構成ファイルで BooleanSwitch を有効または無効にし、構成した BooleanSwitch 値をアプリケーションで使用できます。または、BooleanSwitch をコード内に作成し、Enabled プロパティを直接設定してコードの特定のセクションをインストルメントすることもできます。

BooleanSwitch を構成するには、アプリケーションの名前に対応した構成ファイルを編集します。このファイルでは、スイッチの追加または削除、スイッチの値の設定、アプリケーションで以前設定されたすべてのスイッチのクリアを実行できます。構成ファイルの書式は次の例のとおりです。

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

この例の構成セクションで BooleanSwitch を定義するには、DisplayName プロパティを mySwitch に設定し、Enabled 値を true に設定します。アプリケーション内で、構成したスイッチ値を使用するには、次のコード例に示すように BooleanSwitch を同じ名前で作成します。

Private Shared boolSwitch As
 New BooleanSwitch("mySwitch",
 _
    "Switch in config file")

Public Shared Sub Main(ByVal
 CmdArgs() As String)
    '...
    Console.WriteLine("Boolean switch {0} configured as {1}",
 _
        boolSwitch.DisplayName, boolSwitch.Enabled.ToString())
    If boolSwitch.Enabled Then
        '...
    End If

End Sub
private static BooleanSwitch boolSwitch = new
 BooleanSwitch("mySwitch", 
    "Switch in config file");

public static void Main(string[]
 args) 
{
    //...
    Console.WriteLine("Boolean switch {0} configured as {1}",
 
        boolSwitch.DisplayName, boolSwitch.Enabled.ToString());
    if (boolSwitch.Enabled)
    {
        //...
    }
}

既定では、Enabled プロパティは構成ファイルで指定した値を使用して設定されます。Enabled プロパティを false に設定するにはスイッチに値 0 を設定し、Enabled プロパティを true に設定するにはスイッチを 0 以外に設定します。BooleanSwitch コンストラクタが構成ファイルの初期スイッチ設定を検出できない場合、新しいスイッチの Enabled プロパティは既定で false に設定されます。

スイッチを使用するには、トレースまたはデバッグを有効にする必要があります。次の構文はコンパイラに固有です。C# または Visual Basic 以外のコンパイラを使用する場合は、使用するコンパイラのドキュメントを参照してください。

メモメモ

これらのデバッグおよびトレースのコンパイラ スイッチは、BooleanSwitch クラスを単独で使用する場合には必要ありません。これらが必要になるのは、Trace クラスまたは Debug クラスの条件付きコンパイルされたメソッドと組み合わせて使用する場合だけです。

アプリケーション導入の詳細については、Debug および Trace のトピックを参照してください。トレース スイッチの構成と使用の詳細については、「トレース スイッチ」を参照してください。

メモメモ

クラスの BooleanSwitch のメンバを static にすることで、パフォーマンスを向上させることができます。

使用例使用例

BooleanSwitch を作成し、スイッチを使用してエラー メッセージを出力するかどうかを決定する例を次に示します。スイッチはクラス レベルで作成します。Main メソッドは、その位置をエラー メッセージとエラーの発生場所を出力する MyMethod に渡します。

' Class level declaration.
' Create a BooleanSwitch for data. 
Private Shared dataSwitch As
 New BooleanSwitch("Data", "DataAccess
 module")


Public Shared Sub MyMethod(location
 As String)
    ' Insert code here to handle processing.
    If dataSwitch.Enabled Then
        Console.WriteLine(("Error happened at " +
 location))
    End If
End Sub 'MyMethod

' Entry point which delegates to C-style main function.
Public Overloads Shared
 Sub Main()
    Main(System.Environment.GetCommandLineArgs())
End Sub
 
Overloads Public Shared
 Sub Main(args() As String)
    ' Run the method which writes an error message specifying the location
 of the error.
    MyMethod("in Main")
End Sub 'Main
// Class level declaration.
/* Create a BooleanSwitch for data.*/
static BooleanSwitch dataSwitch = new BooleanSwitch("Data",
 "DataAccess module");

static public void MyMethod(string
 location) {
   //Insert code here to handle processing.
   if(dataSwitch.Enabled)
      Console.WriteLine("Error happened at " + location);
}

public static void Main(string[]
 args) {
   //Run the method which writes an error message specifying the location
 of the error.
   MyMethod("in Main");
}

public ref class BooleanSwitchTest
{
private:

   /* Create a BooleanSwitch for data.*/
   static BooleanSwitch^ dataSwitch = gcnew BooleanSwitch( "Data","DataAccess
 module" );

public:
   static void MyMethod( String^ location )
   {
      
      //Insert code here to handle processing.
      if ( dataSwitch->Enabled )
            Console::WriteLine( "Error happened at {0}", location );
   }

};

int main()
{
   
   //Run the method which writes an error message specifying the location
 of the error.
   BooleanSwitchTest::MyMethod( "in main" );
}

// Class level declaration.
/* Create a BooleanSwitch for data.
 */
private static BooleanSwitch dataSwitch = 
    new BooleanSwitch("Data", "DataAccess module");

public static void MyMethod(String
 location)
{
    //Insert code here to handle processing.
    if (dataSwitch.get_Enabled()) {
        Console.WriteLine("Error happened at " + location);
    }
} //MyMethod

public static void main(String[]
 args)
{
    // Run the method which writes an error message specifying 
    // the location of the error.
    MyMethod("in main");
} //main
// Class level declaration.
 /* Create a BooleanSwitch for data.*/
 static var dataSwitch : BooleanSwitch = new
 BooleanSwitch("Data", "DataAccess module");
 
 static public function
 MyMethod(location : String) {
    // Insert code here to handle processing.
    if(dataSwitch.Enabled)
       Console.WriteLine("Error happened at " + location);
 }
 
 public static function
 Main() {
    // Run the method which writes an error message specifying the location
 of the error.
    MyMethod("in Main");
 }

継承階層継承階層
System.Object
   System.Diagnostics.Switch
    System.Diagnostics.BooleanSwitch
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

BooleanSwitch コンストラクタ (String, String, String)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

スイッチの表示名、説明、および既定のスイッチ値を指定して、BooleanSwitch クラスの新しいインスタンスを初期化します。

名前空間: System.Diagnostics
アセンブリ: System (system.dll 内)
構文構文

Public Sub New ( _
    displayName As String, _
    description As String, _
    defaultSwitchValue As String _
)
Dim displayName As String
Dim description As String
Dim defaultSwitchValue As String

Dim instance As New BooleanSwitch(displayName,
 description, defaultSwitchValue)
public BooleanSwitch (
    string displayName,
    string description,
    string defaultSwitchValue
)
public:
BooleanSwitch (
    String^ displayName, 
    String^ description, 
    String^ defaultSwitchValue
)
public BooleanSwitch (
    String displayName, 
    String description, 
    String defaultSwitchValue
)
public function BooleanSwitch (
    displayName : String, 
    description : String, 
    defaultSwitchValue : String
)

パラメータ

displayName

ユーザー インターフェイスで表示する名前。

description

スイッチの説明。

defaultSwitchValue

スイッチの既定値。

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

BooleanSwitch コンストラクタ


BooleanSwitch コンストラクタ (String, String)

表示名と説明を指定して、BooleanSwitch クラスの新しいインスタンスを初期化します。

名前空間: System.Diagnostics
アセンブリ: System (system.dll 内)
構文構文

解説解説

BooleanSwitch を作成するときは、スイッチの初期設定値の検索に displayName パラメータが使用されます。コンストラクタが初期設定値を見つけることができなかった場合、Enabled プロパティは false (無効) に設定されます。

BooleanSwitch のレベルを設定するには、アプリケーション名に対応する構成ファイルを編集します。このファイル内で、スイッチの追加、スイッチの値の設定、スイッチの削除、そのアプリケーションで以前に設定したすべてのスイッチのクリアなどを行うことができます。構成ファイルの書式は次の例のようになります。

 <configuration>
     <system.diagnostics>
        <switches>
           <add name="mySwitch" value="10" />
           <add name="myNewSwitch" value="20" />
           <remove name="mySwitch" />
           <clear/>
        </switches>
     </system.diagnostics>
  </configuration>
メモメモ

作成したスイッチは static にする必要があります。

使用例使用例

BooleanSwitch を作成し、スイッチを使用してエラー メッセージを出力するかどうかを決定する例を次に示します。スイッチはクラス レベルで作成されます。Main メソッドは、その位置をエラー メッセージとエラーの発生場所を出力する MyMethod に渡します。

' Class level declaration.
' Create a BooleanSwitch for data. 
Private Shared dataSwitch As
 New BooleanSwitch("Data", "DataAccess
 module")


Public Shared Sub MyMethod(location
 As String)
    ' Insert code here to handle processing.
    If dataSwitch.Enabled Then
        Console.WriteLine(("Error happened at " +
 location))
    End If
End Sub 'MyMethod

' Entry point which delegates to C-style main function.
Public Overloads Shared
 Sub Main()
    Main(System.Environment.GetCommandLineArgs())
End Sub
 
Overloads Public Shared
 Sub Main(args() As String)
    ' Run the method which writes an error message specifying the location
 of the error.
    MyMethod("in Main")
End Sub 'Main
// Class level declaration.
/* Create a BooleanSwitch for data.*/
static BooleanSwitch dataSwitch = new BooleanSwitch("Data",
 "DataAccess module");

static public void MyMethod(string
 location) {
   //Insert code here to handle processing.
   if(dataSwitch.Enabled)
      Console.WriteLine("Error happened at " + location);
}

public static void Main(string[]
 args) {
   //Run the method which writes an error message specifying the location
 of the error.
   MyMethod("in Main");
}

public ref class BooleanSwitchTest
{
private:

   /* Create a BooleanSwitch for data.*/
   static BooleanSwitch^ dataSwitch = gcnew BooleanSwitch( "Data","DataAccess
 module" );

public:
   static void MyMethod( String^ location )
   {
      
      //Insert code here to handle processing.
      if ( dataSwitch->Enabled )
            Console::WriteLine( "Error happened at {0}", location );
   }

};

int main()
{
   
   //Run the method which writes an error message specifying the location
 of the error.
   BooleanSwitchTest::MyMethod( "in main" );
}

// Class level declaration.
/* Create a BooleanSwitch for data.
 */
private static BooleanSwitch dataSwitch = 
    new BooleanSwitch("Data", "DataAccess module");

public static void MyMethod(String
 location)
{
    //Insert code here to handle processing.
    if (dataSwitch.get_Enabled()) {
        Console.WriteLine("Error happened at " + location);
    }
} //MyMethod

public static void main(String[]
 args)
{
    // Run the method which writes an error message specifying 
    // the location of the error.
    MyMethod("in main");
} //main
// Class level declaration.
 /* Create a BooleanSwitch for data.*/
 static var dataSwitch : BooleanSwitch = new
 BooleanSwitch("Data", "DataAccess module");
 
 static public function
 MyMethod(location : String) {
    // Insert code here to handle processing.
    if(dataSwitch.Enabled)
       Console.WriteLine("Error happened at " + location);
 }
 
 public static function
 Main() {
    // Run the method which writes an error message specifying the location
 of the error.
    MyMethod("in Main");
 }

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

BooleanSwitch プロパティ


BooleanSwitch プロパティ


BooleanSwitch メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

BooleanSwitch クラス
System.Diagnostics 名前空間
Switch
TraceSwitch
Debug
Trace

BooleanSwitch メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

BooleanSwitch クラス
System.Diagnostics 名前空間
Switch
TraceSwitch
Debug
Trace

BooleanSwitch メンバ

デバッグおよびトレースの出力を制御する単純なオン/オフ スイッチを提供します。

BooleanSwitch データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
パブリック プロパティパブリック プロパティ
プロテクト プロパティプロテクト プロパティ
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

BooleanSwitch クラス
System.Diagnostics 名前空間
Switch
TraceSwitch
Debug
Trace



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「BooleanSwitch」の関連用語

BooleanSwitchのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



BooleanSwitchのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS