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) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「BooleanSwitch クラス」の関連用語

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

   

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



BooleanSwitch クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS