次の方法で共有


BooleanSwitch クラス

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

この型のすべてのメンバの一覧については、BooleanSwitch メンバ を参照してください。

System.Object
   System.Diagnostics.Switch
      System.Diagnostics.BooleanSwitch

Public Class BooleanSwitch
   Inherits Switch
[C#]
public class BooleanSwitch : Switch
[C++]
public __gc class BooleanSwitch : public Switch
[JScript]
public class BooleanSwitch extends Switch

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

BooleanSwitch コンストラクタがスイッチの初期設定値を見つけることができなかった場合、既定により新しいスイッチは無効 (false) に設定されます。 Enabled プロパティを使用すると、スイッチの現在の値を取得できます。

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

  • C# でデバッグを有効にするには、コードのコンパイル時に /d:DEBUG フラグをコンパイラのコマンド ラインに追加するか、 #define DEBUG をファイルの最上部に挿入します。Visual Basic では、コンパイラのコマンド ラインに /d:DEBUG=True フラグを追加します。
  • C# でトレースを有効にするには、コードのコンパイル時に /d:TRACE フラグをコンパイラのコマンド ラインに追加するか、 #define TRACE をファイルの最上部に挿入します。Visual Basic では、コンパイラのコマンド ラインに /d:TRACE=True フラグを追加します。

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

BooleanSwitch のレベルを設定するには、コード内で直接 Enabled プロパティを使用するか、またはアプリケーションの名前に対応する構成ファイルを編集します。構成ファイルを使ってスイッチの値を変更する場合は、ソース コードを再コンパイルする必要はありません。構成ファイルを使用して、スイッチの追加とその値の設定、スイッチの削除、アプリケーションにより設定されていたすべてのスイッチのクリアなどを行うことができます。構成ファイルの書式は次の例のようになります。

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

メモ   パフォーマンス向上のため、クラスで BooleanSwitch メンバを静的 (Visual Basic では Shared) にできます。

使用例

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

[C#] 
// 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");
}


[C++] 
public __gc class BooleanSwitchTest
{
   /* Create a BooleanSwitch for data.*/
   static BooleanSwitch* dataSwitch = new BooleanSwitch(S"Data", S"DataAccess module");

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

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

[JScript] 
// 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.Diagnostics

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System (System.dll 内)

参照

BooleanSwitch メンバ | System.Diagnostics 名前空間 | Switch | TraceSwitch | Debug | Trace