Debug.WriteIf 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
如果條件為 true
,將有關偵錯的資訊寫入 Listeners 集合中的追蹤接聽項。
多載
WriteIf(Boolean, Debug+WriteIfInterpolatedStringHandler) |
如果 |
WriteIf(Boolean, Object) |
如果條件為 |
WriteIf(Boolean, String) |
如果條件為 |
WriteIf(Boolean, Debug+WriteIfInterpolatedStringHandler, String) |
如果指定的條件為 |
WriteIf(Boolean, Object, String) |
如果條件為 |
WriteIf(Boolean, String, String) |
如果條件是 |
WriteIf(Boolean, Debug+WriteIfInterpolatedStringHandler)
- 來源:
- Debug.cs
- 來源:
- Debug.cs
- 來源:
- Debug.cs
如果 condition
為 true
,則會將類別名稱和訊息寫入集合中的 Listeners 追蹤接聽程式。
public:
static void WriteIf(bool condition, System::Diagnostics::Debug::WriteIfInterpolatedStringHandler % message);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteIf (bool condition, ref System.Diagnostics.Debug.WriteIfInterpolatedStringHandler message);
[<System.Diagnostics.Conditional("DEBUG")>]
static member WriteIf : bool * WriteIfInterpolatedStringHandler -> unit
Public Shared Sub WriteIf (condition As Boolean, ByRef message As Debug.WriteIfInterpolatedStringHandler)
參數
- condition
- Boolean
要評估的條件運算式。 如果條件為 true
,則將值寫入集合中的追蹤接聽項。
如果 condition
為 true
,則要寫入的訊息。
- 屬性
備註
這個多載是在 .NET 6 中引進來改善效能。 相較于採用 String
參數的多載,此多載只會在需要訊息時評估任何插補字串格式專案。
根據預設,輸出會寫入 的 DefaultTraceListener 實例。
category
使用 參數來群組輸出訊息。
這個方法會 Write 呼叫追蹤接聽程式的 方法。
另請參閱
- Debug
- Trace
- BooleanSwitch
- TraceSwitch
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- ConditionalAttribute
適用於
WriteIf(Boolean, Object)
- 來源:
- Debug.cs
- 來源:
- Debug.cs
- 來源:
- Debug.cs
如果條件為 true
,則將物件的 ToString() 方法值寫入 Listeners 集合中的追蹤接聽項。
public:
static void WriteIf(bool condition, System::Object ^ value);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteIf (bool condition, object value);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteIf (bool condition, object? value);
[<System.Diagnostics.Conditional("DEBUG")>]
static member WriteIf : bool * obj -> unit
Public Shared Sub WriteIf (condition As Boolean, value As Object)
參數
- condition
- Boolean
要評估的條件運算式。 如果條件為 true
,則將值寫入集合中的追蹤接聽項。
- 屬性
範例
下列範例會 TraceSwitch 建立名為 generalSwitch
的 。 這個參數是在程式碼範例之外設定的。
如果參數設定為 TraceLevelError
或更新版本,則此範例會將 value 參數的名字輸出至 Listeners 。 如需將接聽程式新增至 Listeners 集合的資訊,請參閱 TraceListenerCollection 類別。
然後,如果 TraceLevel 設定 Verbose
為 ,則範例會在與第一個訊息相同的行上輸出訊息。 第二個訊息後面的行結束字元。
// Class-level declaration.
// Create a TraceSwitch.
static TraceSwitch^ generalSwitch =
gcnew TraceSwitch( "General","Entire Application" );
public:
static void MyErrorMethod( Object^ myObject )
{
// Write the message if the TraceSwitch level is set to Error or higher.
#if defined(DEBUG)
Debug::WriteIf( generalSwitch->TraceError, myObject );
// Write a second message if the TraceSwitch level is set to Verbose.
Debug::WriteLineIf( generalSwitch->TraceVerbose,
" is not a valid value for this method." );
#endif
}
// 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, myObject);
// Write a second message if the TraceSwitch level is set to Verbose.
Debug.WriteLineIf(generalSwitch.TraceVerbose, " is not a valid value for this method.");
}
' 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, myObject)
' Write a second message if the TraceSwitch level is set to Verbose.
Debug.WriteLineIf(generalSwitch.TraceVerbose, " is not a valid value for this method.")
End Sub
備註
根據預設,輸出會寫入 的 DefaultTraceListener 實例。
這個方法會 Write 呼叫追蹤接聽程式的 方法。
給繼承者的注意事項
您可以使用 語句而非 using If...Then
WriteIf(Boolean, String) 語句,將檢測應用程式的效能降低降到最低。 下列兩個程式碼範例會傳送相同的偵錯訊息。 不過,當追蹤關閉時,第一個範例會更快,因為如果 mySwitch.TraceError
評估為 false
,則不會呼叫 Write(String) 。 第二個範例一律會呼叫 WriteIf(Boolean, String) ,即使 mySwitch.TraceError
是 false
且不會產生追蹤輸出也一樣。 這可能會導致不必要地執行任意複雜的程式碼。
第一個範例:
if(mySwitch.TraceError)
Debug.Write("aNumber = " + aNumber + " out of range");
第二個範例:
Debug.WriteIf(mySwitch.TraceError, "aNumber = " + aNumber + " out of range");
另請參閱
- Debug
- Trace
- BooleanSwitch
- TraceSwitch
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- ConditionalAttribute
適用於
WriteIf(Boolean, String)
- 來源:
- Debug.cs
- 來源:
- Debug.cs
- 來源:
- Debug.cs
如果條件為 true
,則將訊息寫入 Listeners 集合中的追蹤接聽項。
public:
static void WriteIf(bool condition, System::String ^ message);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteIf (bool condition, string message);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteIf (bool condition, string? message);
[<System.Diagnostics.Conditional("DEBUG")>]
static member WriteIf : bool * string -> unit
Public Shared Sub WriteIf (condition As Boolean, message As String)
參數
- condition
- Boolean
要評估的條件運算式。 如果條件為 true
,則將訊息寫入集合中的追蹤接聽項。
- message
- String
要寫入的訊息。
- 屬性
範例
下列範例會 TraceSwitch 建立名為 generalSwitch
的 。 這個參數是在程式碼範例之外設定的。
如果參數設定為 TraceLevelError
或更新版本,則範例會將第一個錯誤訊息輸出至 Listeners 。 如需將接聽程式新增至 Listeners 集合的詳細資訊,請參閱 TraceListenerCollection 類別。
然後,如果 TraceLevel 設定 Verbose
為 ,則範例會在與第一個訊息相同的一行上輸出第二個錯誤訊息。 第二個訊息後面的行結束字元。
// Class-level declaration.
// Create a TraceSwitch.
static TraceSwitch^ generalSwitch =
gcnew TraceSwitch( "General","Entire Application" );
public:
static void MyErrorMethod()
{
// Write the message if the TraceSwitch level is set to Error or higher.
#if defined(DEBUG)
Debug::WriteIf( generalSwitch->TraceError, "My error message. " );
// Write a second message if the TraceSwitch level is set to Verbose.
Debug::WriteIf( generalSwitch->TraceVerbose,
"My second error message." );
#endif
}
// Class-level declaration.
// Create a TraceSwitch.
TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application");
static void MyErrorMethod()
{
// Write the message if the TraceSwitch level is set to Error or higher.
Debug.WriteIf(generalSwitch.TraceError, "My error message. ");
// Write a second message if the TraceSwitch level is set to Verbose.
Debug.WriteIf(generalSwitch.TraceVerbose, "My second error message.");
}
' Class-level declaration.
' Create a TraceSwitch.
Private Shared generalSwitch As New TraceSwitch("General", "Entire Application")
Public Shared Sub MyErrorMethod()
' Write the message if the TraceSwitch level is set to Error or higher.
Debug.WriteIf(generalSwitch.TraceError, "My error message. ")
' Write a second message if the TraceSwitch level is set to Verbose.
Debug.WriteIf(generalSwitch.TraceVerbose, "My second error message.")
End Sub
備註
根據預設,輸出會寫入 的 DefaultTraceListener 實例。
這個方法會 Write 呼叫追蹤接聽程式的 方法。
給繼承者的注意事項
您可以使用 語句而非 using If...Then
WriteIf(Boolean, String) 語句,將檢測應用程式的效能降低降到最低。 下列兩個程式碼範例會傳送相同的偵錯訊息。 不過,當追蹤關閉時,第一個範例會更快,因為如果 mySwitch.TraceError
評估為 false
,則不會呼叫 Write(String) 。 第二個範例一律會呼叫 WriteIf(Boolean, String) ,即使 mySwitch.TraceError
是 false
且不會產生追蹤輸出也一樣。 這可能會導致不必要地執行任意複雜的程式碼。
第一個範例:
if(mySwitch.TraceError)
Debug.Write("aNumber = " + aNumber + " out of range");
第二個範例:
Debug.WriteIf(mySwitch.TraceError, "aNumber = " + aNumber + " out of range");
另請參閱
- Debug
- Trace
- BooleanSwitch
- TraceSwitch
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- ConditionalAttribute
適用於
WriteIf(Boolean, Debug+WriteIfInterpolatedStringHandler, String)
- 來源:
- Debug.cs
- 來源:
- Debug.cs
- 來源:
- Debug.cs
如果指定的條件為 true
,將類別名稱和訊息寫入集合中的 Listeners 追蹤接聽程式。
public:
static void WriteIf(bool condition, System::Diagnostics::Debug::WriteIfInterpolatedStringHandler % message, System::String ^ category);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteIf (bool condition, ref System.Diagnostics.Debug.WriteIfInterpolatedStringHandler message, string? category);
[<System.Diagnostics.Conditional("DEBUG")>]
static member WriteIf : bool * WriteIfInterpolatedStringHandler * string -> unit
Public Shared Sub WriteIf (condition As Boolean, ByRef message As Debug.WriteIfInterpolatedStringHandler, category As String)
參數
- condition
- Boolean
要評估的條件運算式。 如果條件為 true
,則將訊息寫入集合中的追蹤接聽項。
要寫入的訊息。
- category
- String
用來組織輸出的類別名稱。
- 屬性
備註
這個多載是在 .NET 6 中引進來改善效能。 相較于採用 String
參數的多載,此多載只會在需要訊息時評估任何插補字串格式專案。
根據預設,輸出會寫入 的 DefaultTraceListener 實例。
category
使用 參數來群組輸出訊息。
這個方法會 Write 呼叫追蹤接聽程式的 方法。
另請參閱
- Debug
- Trace
- BooleanSwitch
- TraceSwitch
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- ConditionalAttribute
適用於
WriteIf(Boolean, Object, String)
- 來源:
- Debug.cs
- 來源:
- Debug.cs
- 來源:
- Debug.cs
如果條件為 true
,則將物件之 ToString() 方法的分類名稱和值寫入 Listeners 集合中的追蹤接聽項。
public:
static void WriteIf(bool condition, System::Object ^ value, System::String ^ category);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteIf (bool condition, object value, string category);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteIf (bool condition, object? value, string? category);
[<System.Diagnostics.Conditional("DEBUG")>]
static member WriteIf : bool * obj * string -> unit
Public Shared Sub WriteIf (condition As Boolean, value As Object, category As String)
參數
- condition
- Boolean
要評估的條件運算式。 如果條件為 true
,則將分類名稱和值寫入集合中的追蹤接聽項。
- category
- String
用來組織輸出的類別名稱。
- 屬性
範例
下列範例會 TraceSwitch 建立名為 generalSwitch
的 。 這個參數是在程式碼範例之外設定的。
如果 參數設定為 TraceLevelVerbose
,則範例會將 的名稱 myObject
和 category
輸出至 Listeners 。 如需將接聽程式新增至 Listeners 集合的資訊,請參閱 TraceListenerCollection 類別。
然後,如果 TraceLevel 設定為 Error
或更新版本,則範例會在與第一個訊息相同的一行上輸出第二個錯誤訊息。 第二個訊息後面的行結束字元。
// Class-level declaration.
// Create a TraceSwitch.
static TraceSwitch^ generalSwitch =
gcnew TraceSwitch( "General","Entire Application" );
public:
static void MyErrorMethod( Object^ myObject, String^ category )
{
// Write the message if the TraceSwitch level is set to Error or higher.
#if defined(DEBUG)
Debug::WriteIf( generalSwitch->TraceVerbose, myObject, category );
// Write a second message if the TraceSwitch level is set to Verbose.
Debug::WriteLineIf( generalSwitch->TraceError,
" Object is not valid for this category." );
#endif
}
// Class-level declaration.
// Create a TraceSwitch.
static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application");
static public void MyErrorMethod(Object myObject, string category)
{
// Write the message if the TraceSwitch level is set to Verbose.
Debug.WriteIf(generalSwitch.TraceVerbose, myObject, category);
// Write a second message if the TraceSwitch level is set to Error or higher.
Debug.WriteLineIf(generalSwitch.TraceError, " Object is not valid for this category.");
}
' Class-level declaration.
' Create a TraceSwitch.
Private Shared generalSwitch As New TraceSwitch("General", "Entire Application")
Public Shared Sub MyErrorMethod(myObject As Object, category As String)
' Write the message if the TraceSwitch level is set to Verbose.
Debug.WriteIf(generalSwitch.TraceVerbose, myObject, category)
' Write a second message if the TraceSwitch level is set to Error or higher.
Debug.WriteLineIf(generalSwitch.TraceError, " Object is not valid for this category.")
End Sub
備註
根據預設,輸出會寫入 的 DefaultTraceListener 實例。
參數 category
可用來將輸出訊息分組。
這個方法會 Write 呼叫追蹤接聽程式的 方法。
給繼承者的注意事項
您可以使用 語句而非 using If...Then
WriteIf(Boolean, String) 語句,將檢測應用程式的效能降低降到最低。 下列兩個程式碼範例會傳送相同的偵錯訊息。 不過,當追蹤關閉時,第一個範例會更快,因為如果 mySwitch.TraceError
評估為 false
,則不會呼叫 Write(String) 。 第二個範例一律會呼叫 WriteIf(Boolean, String) ,即使 mySwitch.TraceError
是 false
且不會產生追蹤輸出也一樣。 這可能會導致不必要地執行任意複雜的程式碼。
第一個範例:
if(mySwitch.TraceError)
Debug.Write("aNumber = " + aNumber + " out of range");
第二個範例:
Debug.WriteIf(mySwitch.TraceError, "aNumber = " + aNumber + " out of range");
另請參閱
- Debug
- Trace
- BooleanSwitch
- TraceSwitch
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- ConditionalAttribute
適用於
WriteIf(Boolean, String, String)
- 來源:
- Debug.cs
- 來源:
- Debug.cs
- 來源:
- Debug.cs
如果條件是 true
,則將分類名稱和訊息寫入 Listeners 集合中的追蹤接聽項。
public:
static void WriteIf(bool condition, System::String ^ message, System::String ^ category);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteIf (bool condition, string message, string category);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteIf (bool condition, string? message, string? category);
[<System.Diagnostics.Conditional("DEBUG")>]
static member WriteIf : bool * string * string -> unit
Public Shared Sub WriteIf (condition As Boolean, message As String, category As String)
參數
- condition
- Boolean
要評估的條件運算式。 如果條件為 true
,則將分類名稱和訊息寫入集合中的追蹤接聽項。
- message
- String
要寫入的訊息。
- category
- String
用來組織輸出的類別名稱。
- 屬性
範例
下列範例會 TraceSwitch 建立名為 generalSwitch
的 。 此參數是在程式碼範例外部設定。
如果參數設定為 TraceLevelVerbose
,則範例會將第一個錯誤訊息輸出至 Listeners 。 如需將接聽程式新增至 Listeners 集合的資訊,請參閱 類別 TraceListenerCollection 。
然後,如果 TraceLevel 設定為 Error
或更新版本,則此範例會在與第一個訊息相同的行上輸出第二個錯誤訊息。 行結束字元會接在第二個訊息後面。
// Class-level declaration.
// Create a TraceSwitch.
static TraceSwitch^ generalSwitch =
gcnew TraceSwitch( "General","Entire Application" );
public:
static void MyErrorMethod( Object^ myObject, String^ category )
{
// Write the message if the TraceSwitch level is set to Error or higher.
#if defined(DEBUG)
Debug::WriteIf( generalSwitch->TraceVerbose, String::Concat( myObject,
" is not a valid object for category: " ), category );
// Write a second message if the TraceSwitch level is set to Verbose.
Debug::WriteLineIf( generalSwitch->TraceError,
" Please use a different category." );
#endif
}
// Class-level declaration.
// Create a TraceSwitch.
static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application");
static public void MyErrorMethod(Object myObject, string category)
{
// Write the message if the TraceSwitch level is set to Verbose.
Debug.WriteIf(generalSwitch.TraceVerbose, myObject.ToString() +
" is not a valid object for category: ", category);
// Write a second message if the TraceSwitch level is set to Error or higher.
Debug.WriteLineIf(generalSwitch.TraceError, " Please use a different category.");
}
' Class-level declaration.
' Create a TraceSwitch.
Private Shared generalSwitch As New TraceSwitch("General", "Entire Application")
Public Shared Sub MyErrorMethod(myObject As Object, category As String)
' Write the message if the TraceSwitch level is set to Verbose.
Debug.WriteIf(generalSwitch.TraceVerbose, myObject.ToString() & _
" is not a valid object for category: ", category)
' Write a second message if the TraceSwitch level is set to Error or higher.
Debug.WriteLineIf(generalSwitch.TraceError, " Please use a different category.")
End Sub
備註
根據預設,輸出會寫入 的 DefaultTraceListener 實例。
參數 category
可用來將輸出訊息分組。
這個方法會 TraceListener.Write 呼叫追蹤接聽程式的 方法。
給繼承者的注意事項
您可以使用 語句而非 using If...Then
WriteIf(Boolean, String) 語句,將檢測應用程式的效能降低降到最低。 下列兩個程式碼範例會傳送相同的偵錯訊息。 不過,當追蹤關閉時,第一個範例會更快,因為如果 mySwitch.TraceError
評估為 false
,則不會呼叫 Write(String) 。 第二個範例一律會呼叫 WriteIf(Boolean, String) ,即使 mySwitch.TraceError
是 false
且不會產生追蹤輸出也一樣。 這可能會導致不必要地執行任意複雜的程式碼。
第一個範例:
if(mySwitch.TraceError)
Debug.Write("aNumber = " + aNumber + " out of range");
第二個範例:
Debug.WriteIf(mySwitch.TraceError, "aNumber = " + aNumber + " out of range");
另請參閱
- Debug
- Trace
- BooleanSwitch
- TraceSwitch
- TraceListener
- DefaultTraceListener
- ConsoleTraceListener
- ConditionalAttribute
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應