BooleanSwitch クラス
アセンブリ: System (system.dll 内)


ブール型トレースを使用し、メッセージの重要性に基づいてメッセージの有効と無効を切り替えることができます。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 以外のコンパイラを使用する場合は、使用するコンパイラのドキュメントを参照してください。
-
C# でデバッグを有効にするには、コードのコンパイル時に /d:DEBUG フラグをコンパイラのコマンド ラインに追加するか、#define DEBUG をファイルの最上部に挿入します。Visual Basic では、コンパイラのコマンド ラインに /d:DEBUG=True フラグを追加します。
-
C# でトレースを有効にするには、コードのコンパイル時に /d:TRACE フラグをコンパイラのコマンド ラインに追加するか、#define TRACE をファイルの最上部に挿入します。Visual Basic では、コンパイラのコマンド ラインに /d:TRACE=True フラグを追加します。
![]() |
---|
これらのデバッグおよびトレースのコンパイラ スイッチは、BooleanSwitch クラスを単独で使用する場合には必要ありません。これらが必要になるのは、Trace クラスまたは Debug クラスの条件付きコンパイルされたメソッドと組み合わせて使用する場合だけです。 |
アプリケーション導入の詳細については、Debug および Trace のトピックを参照してください。トレース スイッチの構成と使用の詳細については、「トレース スイッチ」を参照してください。

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.Diagnostics.Switch
System.Diagnostics.BooleanSwitch


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- BooleanSwitch クラスのページへのリンク