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


スイッチは、外部設定を使用して、実行時にトレース出力やデバッグ出力を制御するための効率的な機構を提供します。Switch クラスは、スイッチの既定の動作を実装します。スイッチ レベルは、実行時に変更できます。
このクラスは、BooleanSwitch、SourceSwitch、および TraceSwitch の各クラスの基本クラスです。これらのスイッチで、ほとんどのデバッグおよびトレースの要件を満たすことができます。固有のスイッチを作成する場合は、static にする必要があります。
スイッチを使用するには、トレースまたはデバッグを有効にする必要があります。次の構文はコンパイラに固有です。C# または Visual Basic 以外のコンパイラを使用する場合は、使用するコンパイラのドキュメントを参照してください。
-
C# でデバッグを有効にするには、コードのコンパイル時に /d:DEBUG フラグをコンパイラのコマンド ラインに追加するか、#define DEBUG をファイルの最上部に挿入します。Visual Basic では、コンパイラのコマンド ラインに /d:DEBUG=True フラグを追加します。
-
C# でトレースを有効にするには、コードのコンパイル時に /d:TRACE フラグをコンパイラのコマンド ラインに追加するか、#define TRACE をファイルの最上部に挿入します。Visual Basic では、コンパイラのコマンド ラインに /d:TRACE=True フラグを追加します。
スイッチのレベルを設定するには、アプリケーションの名前に対応する構成ファイルを編集します。このファイルでは、スイッチの追加、その値の設定、スイッチの削除、アプリケーションで以前設定されたすべてのスイッチのクリアを実行できます。構成ファイルの書式は次の例のようになります。
<configuration> <system.diagnostics> <switches> <add name="mySwitch" value="10" /> <add name="myNewSwitch" value="20" /> <remove name="mySwitch" /> <clear/> </switches> </system.diagnostics> </configuration>
![]() |
---|

コール スタックのトレースに使用できる 4 段階のトレース レベルを持つ新しい Switch クラスを定義する方法の例を次に示します。このスイッチを使用すると、メソッドが呼び出されるたび、またはメソッドから制御が戻るたびに、アプリケーションでログを作成できます。
最初の例では、スイッチのレベルを設定するために使用する列挙体を作成します。
' The following are possible values for the new switch. Public Enum MethodTracingSwitchLevel Off = 0 EnteringMethod = 1 ExitingMethod = 2 Both = 3 End Enum 'MethodTracingSwitchLevel
// The following are possible values for the new switch. public enum MethodTracingSwitchLevel { Off = 0, EnteringMethod = 1, ExitingMethod = 2, Both = 3, }
// The following are possible values for the new switch. public enum class MethodTracingSwitchLevel { Off = 0, EnteringMethod = 1, ExitingMethod = 2, Both = 3 };
// The following are possible values for the new switch. public class MethodTracingSwitchLevel { private int member; MethodTracingSwitchLevel() { member = 0; }//MethodTracingSwitchLevel MethodTracingSwitchLevel(int n) { member = n; }//MethodTracingSwitchLevel public int get_Member() { return member; }//get_Member public static int off = 0; public static int enteringMethod = 1; public static int exitingMethod = 2; public static int both = 3; } //MethodTracingSwitchLevel
新しいスイッチを作成する例を次に示します。このコードは、新しいスイッチの値を設定する Level プロパティを実装します。Level は、新しいスイッチに値を割り当てるプロテクト プロパティ SwitchSetting を呼び出します。また、この例では、スイッチに代入した値を取得する 2 つのアクセサ プロパティも実装します。
Public Class MyMethodTracingSwitch Inherits Switch Protected outExit As Boolean Protected outEnter As Boolean Protected myLevel As MethodTracingSwitchLevel Public Sub New(displayName As String, description As String) MyBase.New(displayName, description) End Sub 'New Public Property Level() As MethodTracingSwitchLevel Get Return myLevel End Get Set SetSwitchSetting(CInt(value)) End Set End Property Protected Sub SetSwitchSetting(value As Integer) If value < 0 Then value = 0 End If If value > 3 Then value = 3 End If myLevel = CType(value, MethodTracingSwitchLevel) outEnter = False If value = CInt(MethodTracingSwitchLevel.EnteringMethod) Or _ value = CInt(MethodTracingSwitchLevel.Both) Then outEnter = True End If outExit = False If value = CInt(MethodTracingSwitchLevel.ExitingMethod) Or _ value = CInt(MethodTracingSwitchLevel.Both) Then outExit = True End If End Sub 'SetSwitchSetting Public ReadOnly Property OutputExit() As Boolean Get Return outExit End Get End Property Public ReadOnly Property OutputEnter() As Boolean Get Return outEnter End Get End Property End Class 'MyMethodTracingSwitch
public class MyMethodTracingSwitch:Switch { protected bool outExit; protected bool outEnter; protected MethodTracingSwitchLevel level; public MyMethodTracingSwitch(string displayName, string description):base(displayName, description){ } public MethodTracingSwitchLevel Level { get{ return level; } set{ SetSwitchSetting((int)value); } } protected void SetSwitchSetting(int value){ if(value<0){ value = 0; } if(value>3){ value = 3; } level = (MethodTracingSwitchLevel)value; outEnter = false; if((value == (int)MethodTracingSwitchLevel.EnteringMethod) || (value == (int)MethodTracingSwitchLevel.Both)){ outEnter = true; } outExit = false; if((value == (int)MethodTracingSwitchLevel.ExitingMethod) || (value == (int)MethodTracingSwitchLevel.Both)){ outExit = true; } } public bool OutputExit{ get{ return outExit; } } public bool OutputEnter{ get{ return outEnter; } } }
public ref class MyMethodTracingSwitch: public Switch { protected: bool outExit; bool outEnter; MethodTracingSwitchLevel level; public: MyMethodTracingSwitch( String^ displayName, String^ description ) : Switch( displayName, description ) {} property MethodTracingSwitchLevel Level { MethodTracingSwitchLevel get() { return level; } void set( MethodTracingSwitchLevel value ) { SetSwitchSetting( (int)value ); } } protected: void SetSwitchSetting( int value ) { if ( value < 0 ) { value = 0; } if ( value > 3 ) { value = 3; } level = (MethodTracingSwitchLevel)value; outEnter = false; if ( (value == (int)MethodTracingSwitchLevel::EnteringMethod) || (value == (int)MethodTracingSwitchLevel::Both) ) { outEnter = true; } outExit = false; if ( (value == (int)MethodTracingSwitchLevel::ExitingMethod) || (value == (int)MethodTracingSwitchLevel::Both) ) { outExit = true; } } public: property bool OutputExit { bool get() { return outExit; } } property bool OutputEnter { bool get() { return outEnter; } } };
public class MyMethodTracingSwitch extends Switch { protected boolean outExit; protected boolean outEnter; protected MethodTracingSwitchLevel level; public MyMethodTracingSwitch(String displayName, String description) { super(displayName, description); } //MyMethodTracingSwitch /** @property */ public MethodTracingSwitchLevel get_Level() { return level; }//get_Level /** @property */ public void set_Level(MethodTracingSwitchLevel value) { SetSwitchSetting(value.get_Member()); }//set_Level protected void SetSwitchSetting(int value) { if (value < 0) { value = 0; } if (value > 3) { value = 3; } level = new MethodTracingSwitchLevel(value); outEnter = false; if (value == (int)(MethodTracingSwitchLevel.enteringMethod) || value == (int)(MethodTracingSwitchLevel.both)) { outEnter = true; } outExit = false; if (value == (int)(MethodTracingSwitchLevel.exitingMethod) || value == (int)(MethodTracingSwitchLevel.both)) { outExit = true; } } //SetSwitchSetting /** @property */ public boolean get_OutputExit() { return outExit; }//get_OutputExit /** @property */ public boolean get_OutputEnter() { return outEnter; }//get_OutputEnter } //MyMethodTracingSwitch
新しい Main を作成する例を次に示します。この例では、新しいスイッチを作成し、値を割り当てます。その後、スイッチの設定に応じて、メソッドの呼び出しおよび終了についてのデバッグ メッセージを出力します。
Public Class Class1 ' Create an instance of MyMethodTracingSwitch. Private Shared mySwitch As New _ MyMethodTracingSwitch("Methods", "Trace entering and exiting method") Public Shared Sub Main() ' Write a diagnostic message if the switch is set to entering. Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main") ' Insert code to handle processing. ' Write another diagnostic message if the switch is set to exiting. Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main") End Sub End Class 'MyClass
public class MyClass { /* Create an instance of MyMethodTracingSwitch.*/ static MyMethodTracingSwitch mySwitch = new MyMethodTracingSwitch("Methods", "Trace entering and exiting method"); public static int Main(string[] args) { // Write a diagnostic message if the switch is set to entering. Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main"); // Insert code to handle processing. // Write another diagnostic message if the switch is set to exiting. Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main"); return 0; } }
public ref class MyClass { private: /* Create an instance of MyMethodTracingSwitch.*/ static MyMethodTracingSwitch^ mySwitch = gcnew MyMethodTracingSwitch( "Methods","Trace entering and exiting method" ); public: static int main() { // Write a diagnostic message if the switch is set to entering. Debug::WriteLineIf( mySwitch->OutputEnter, "Entering Main" ); // Insert code to handle processing. // Write another diagnostic message if the switch is set to exiting. Debug::WriteLineIf( mySwitch->OutputExit, "Exiting Main" ); return 0; } };
public class MyClass { /* Create an instance of MyMethodTracingSwitch. */ private static MyMethodTracingSwitch mySwitch = new MyMethodTracingSwitch( "Methods", "Trace entering and exiting method"); public static void main(String[] args) { // Write a diagnostic message if the switch is set to entering. Debug.WriteLineIf(mySwitch.get_OutputEnter(), "Entering main"); // Insert code to handle processing. // Write another diagnostic message if the switch is set to exiting. Debug.WriteLineIf(mySwitch.get_OutputExit(), "Exiting main"); return; } //main } //MyClass

System.Diagnostics.Switch
System.Diagnostics.BooleanSwitch
System.Diagnostics.SourceSwitch
System.Diagnostics.TraceSwitch


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


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