次の方法で共有


Debug.WriteLineIf メソッド (Boolean, Object)

条件が true の場合、オブジェクトの ToString メソッドの値を、 Listeners コレクションのトレース リスナに書き込みます。

<Conditional("DEBUG")>
Overloads Public Shared Sub WriteLineIf( _   ByVal condition As Boolean, _   ByVal value As Object _)
[C#]
[Conditional("DEBUG")]
public static void WriteLineIf(boolcondition,objectvalue);
[C++]
[Conditional("DEBUG")]
public: static void WriteLineIf(boolcondition,Object* value);
[JScript]
public
   Conditional("DEBUG")
static function WriteLineIf(condition : Boolean,value : Object);

パラメータ

  • condition
    メッセージを書き込む場合は true 。それ以外の場合は false
  • value
    名前が Listeners に送信されるオブジェクト。

解説

既定では、出力は DefaultTraceListener のインスタンスに書き込まれます。

このメソッドは、トレース リスナの WriteLine メソッドを呼び出します。

実装時の注意: WriteLineIf ステートメントの代わりに If...Then ステートメントを使用することによって、アプリケーション導入によるパフォーマンスの低下を最小限にとどめることができます。次の 2 つのコード例は、同じデバッグ メッセージを送信します。しかし、トレースがオフである場合は、最初の例の方がはるかに高速です。これは、 mySwitch.TraceErrorfalse として評価されると、 WriteLine を呼び出さないからです。第 2 の例では、 mySwitch.TraceErrorfalse でトレース出力が生成されない場合でも、常に WriteLineIf が呼び出されます。これにより、必要以上に複雑なコードが、不必要に実行される可能性があります。

最初の例。

if(mySwitch.TraceError) 
    Debug.WriteLine("aNumber = " + aNumber + " out of range");

第 2 の例。

Debug.WriteLineIf(mySwitch.TraceError, "aNumber = " + aNumber + " out of range");

使用例

generalSwitch という名前の TraceSwitch を作成する例を次に示します。このスイッチは、コード サンプルの外部で設定されています。

スイッチが TraceLevel Error 以上に設定されている場合、この例では、最初のエラー メッセージが Listeners に出力されます。 Listeners コレクションにリスナを追加する方法については、 TraceListenerCollection クラスのトピックを参照してください。

TraceLevelVerbose に設定されている場合、この例では、最初のメッセージと同じ行にオブジェクトの名前が表示されます。第 2 のメッセージの後に行終端記号があります。

 
' Class-level declaration.
' Create a TraceSwitch.
Private Shared generalSwitch As New TraceSwitch("General", "Entire Application")


Public Shared Sub MyErrorMethod(myObject As Object)
    ' Write the message if the TraceSwitch level is set to Error or higher.
    Debug.WriteIf(generalSwitch.TraceError, "Invalid object. ")
    
    ' Write a second message if the TraceSwitch level is set to Verbose.
    Debug.WriteLineIf(generalSwitch.TraceVerbose, myObject)
End Sub 'MyErrorMethod

[C#] 
// Class-level declaration.
 // Create a TraceSwitch.
 static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application");
 
 static public void MyErrorMethod(Object myObject) {
    // Write the message if the TraceSwitch level is set to Error or higher.
    Debug.WriteIf(generalSwitch.TraceError, "Invalid object. ");
 
    // Write a second message if the TraceSwitch level is set to Verbose.
    Debug.WriteLineIf(generalSwitch.TraceVerbose, myObject);
 }


[C++] 
// Class-level declaration.
 // Create a TraceSwitch.
 static TraceSwitch* generalSwitch = new TraceSwitch(S"General", S"Entire Application");
 
public:
 static void MyErrorMethod(Object* myObject) {
    // Write the message if the TraceSwitch level is set to Error or higher.
    Debug::WriteIf(generalSwitch->TraceError, S"Invalid object. ");
 
    // Write a second message if the TraceSwitch level is set to Verbose.
    Debug::WriteLineIf(generalSwitch->TraceVerbose, myObject);
 }


[JScript] 
// Class level declaration.
 // Create a TraceSwitch.
 static var generalSwitch : TraceSwitch = new TraceSwitch("General", "Entire Application");
 
 static public function MyErrorMethod(myObject : Object) {
    // Write the message if the TraceSwitch level is set to Error or higher.
    Debug.WriteIf(generalSwitch.TraceError, "Invalid object. ");
 
    //Write a second message if the TraceSwitch level is set to Verbose.
    Debug.WriteLineIf(generalSwitch.TraceVerbose, myObject);
 }

必要条件

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

参照

Debug クラス | Debug メンバ | System.Diagnostics 名前空間 | Debug.WriteLineIf オーバーロードの一覧 | Debug | Trace | BooleanSwitch | TraceSwitch | TraceListener | DefaultTraceListener | ConditionalAttribute | ConditionalAttribute | ConditionalAttribute | ConditionalAttribute