TraceSwitch.Level プロパティ

定義

スイッチが許可するメッセージを決定するトレース レベルを取得または設定します。

public:
 property System::Diagnostics::TraceLevel Level { System::Diagnostics::TraceLevel get(); void set(System::Diagnostics::TraceLevel value); };
public System.Diagnostics.TraceLevel Level { get; set; }
member this.Level : System.Diagnostics.TraceLevel with get, set
Public Property Level As TraceLevel

プロパティ値

スイッチによって TraceLevel 許可されるメッセージのレベルを指定する値の 1 つ。

例外

Level が、TraceLevel 値の 1 つではない値に設定されています。

次のコード例では、新しい TraceSwitch を作成し、 スイッチを使用してエラー メッセージを出力するかどうかを判断します。 スイッチはクラス レベルで作成されます。 MyMethod プロパティが 以上に設定されている場合、最初の Level エラー メッセージが TraceLevel.Error 書き込まれます。 ただし、 MyMethod が 未満TraceLevel.Verboseの場合、2 番目のLevelエラー メッセージは書き込まれません。

   // Class-level declaration.
   /* Create a TraceSwitch to use in the entire application.*/
private:
   static TraceSwitch^ mySwitch = gcnew TraceSwitch( "mySwitch","Entire Application" );

public:
   static void MyMethod()
   {
      // Write the message if the TraceSwitch level is set to Error or higher.
      if ( mySwitch->TraceError )
            Console::WriteLine( "My error message." );

      // Write the message if the TraceSwitch level is set to Verbose.
      if ( mySwitch->TraceVerbose )
            Console::WriteLine( "My second error message." );
   }

   static void main()
   {
      // Run the method that prints error messages based on the switch level.
      MyMethod();
   }
//Class-level declaration.
/* Create a TraceSwitch to use in the entire application.*/

static TraceSwitch mySwitch = new TraceSwitch("mySwitch", "Entire Application");

static public void MyMethod()
{
    // Write the message if the TraceSwitch level is set to Error or higher.
    if (mySwitch.TraceError)
        Console.WriteLine("My error message.");

    // Write the message if the TraceSwitch level is set to Verbose.
    if (mySwitch.TraceVerbose)
        Console.WriteLine("My second error message.");
}

public static void Main(string[] args)
{
    // Run the method that prints error messages based on the switch level.
    MyMethod();
}
' Class-level declaration.
' Create a TraceSwitch to use in the entire application. 

Private Shared mySwitch As New TraceSwitch("mySwitch", "Entire Application")

Public Shared Sub MyMethod()
    ' Write the message if the TraceSwitch level is set to Error or higher.
    If mySwitch.TraceError Then
        Console.WriteLine("My error message.")
    End If 
    ' Write the message if the TraceSwitch level is set to Verbose.
    If mySwitch.TraceVerbose Then
        Console.WriteLine("My second error message.")
    End If
End Sub

Public Shared Sub Main()
    ' Run the method that prints error messages based on the switch level.
    MyMethod()
End Sub

注釈

.NET Framework アプリの場合、 のレベルをTraceSwitch設定するには、アプリケーションの名前に対応する構成ファイルを編集します。 このファイルでは、スイッチを追加してその値を設定したり、スイッチを削除したり、アプリケーションによって以前に設定されたすべてのスイッチをクリアすることができます。 構成ファイルは、次の例のように書式設定する必要があります。

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

テキストを使用してスイッチの値を指定することもできます。 たとえば、 true の 場合BooleanSwitchや、 などのError列挙値を表すテキスト。TraceSwitch <add name="mySwitch" value="Error" /> という行は、<add name="mySwitch" value="1" /> と同じです。

プロパティの Level 既定値は です TraceLevel.Off。 または、.NET Framework アプリの場合、レベルは構成ファイルから取得されます (使用可能な場合)。

このプロパティを設定すると、TraceErrorTraceWarning、、TraceInfo、および TraceVerbose の各プロパティが更新され、新しい値が反映されます。

適用対象

こちらもご覧ください