Trace.WriteIf 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
如果条件为 true
,则将有关跟踪的信息写入 Listeners 集合中的跟踪侦听器。
重载
WriteIf(Boolean, String, String) |
如果条件为 |
WriteIf(Boolean, Object, String) |
如果条件为 |
WriteIf(Boolean, Object) |
如果条件为 |
WriteIf(Boolean, String) |
如果条件为 |
WriteIf(Boolean, String, String)
- Source:
- Trace.cs
- Source:
- Trace.cs
- Source:
- Trace.cs
如果条件为 true
,则将类别名称和消息写入 Listeners 集合中的跟踪侦听器。
public:
static void WriteIf(bool condition, System::String ^ message, System::String ^ category);
[System.Diagnostics.Conditional("TRACE")]
public static void WriteIf (bool condition, string? message, string? category);
[System.Diagnostics.Conditional("TRACE")]
public static void WriteIf (bool condition, string message, string category);
[<System.Diagnostics.Conditional("TRACE")>]
static member WriteIf : bool * string * string -> unit
Public Shared Sub WriteIf (condition As Boolean, message As String, category As String)
参数
- condition
- Boolean
若要使消息被写入,则为 true
;否则为 false
。
- message
- String
要写入的消息。
- category
- String
用于组织输出的类别名称。
- 属性
示例
以下示例创建一个名为 TraceSwitch 的 generalSwitch
。 此开关在代码示例外部设置。
如果开关设置为 , TraceLevelVerbose
则示例将第一条错误消息输出到 Listeners。 有关将侦听器添加到集合的信息 Listeners ,请参阅 TraceListenerCollection 类。
然后,如果 TraceLevel 设置为 Error
或更高,则示例在与第一条消息相同的行上输出第二条错误消息。 第二条消息后跟行终止符。
// Class-level declaration.
// Create a TraceSwitch.
private:
static TraceSwitch^ generalSwitch =
gcnew TraceSwitch( "General", "Entire Application" );
public:
static void MyErrorMethod( Object^ myObject, String^ category )
{
#if defined(TRACE)
// Write the message if the TraceSwitch level is set to Verbose.
Trace::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 Error or higher.
Trace::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.
Trace.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.
Trace.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.
Trace.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.
Trace.WriteLineIf(generalSwitch.TraceError, _
" Please use a different category.")
End Sub
注解
默认情况下,输出将写入 实例 DefaultTraceListener。
参数 category
可用于对输出消息进行分组。
此方法调用 Write 跟踪侦听器的 方法。
继承者说明
可以使用 语句而不是 WriteIf(Boolean, String) using 语句来最大程度地降低检测应用程序If...Then
的性能损失。 以下两个代码示例发送相同的调试消息。 但是,当跟踪关闭时,第一个示例要快得多,因为如果 mySwitch.TraceError
计算结果为 false
,则不会调用 Write(String)。 第二个示例始终调用 WriteIf(Boolean, String),即使 为 false
mySwitch.TraceError
且未生成跟踪输出。 这可能会导致不必要地执行任意复杂的代码。
第一个示例
if(mySwitch.TraceError)
Trace.Write("aNumber = " + aNumber + " out of range");
第二个示例
Trace.WriteIf(mySwitch.TraceError, "aNumber = " + aNumber + " out of range");
另请参阅
适用于
WriteIf(Boolean, Object, String)
- Source:
- Trace.cs
- Source:
- Trace.cs
- Source:
- Trace.cs
如果条件为 true
,则向 Listeners 集合中的跟踪侦听器写入类别名称和对象的 ToString() 方法值。
public:
static void WriteIf(bool condition, System::Object ^ value, System::String ^ category);
[System.Diagnostics.Conditional("TRACE")]
public static void WriteIf (bool condition, object? value, string? category);
[System.Diagnostics.Conditional("TRACE")]
public static void WriteIf (bool condition, object value, string category);
[<System.Diagnostics.Conditional("TRACE")>]
static member WriteIf : bool * obj * string -> unit
Public Shared Sub WriteIf (condition As Boolean, value As Object, category As String)
参数
- condition
- Boolean
若要使消息被写入,则为 true
;否则为 false
。
- category
- String
用于组织输出的类别名称。
- 属性
示例
以下示例创建一个名为 TraceSwitch 的 generalSwitch
。 此开关在代码示例外部设置。
如果开关设置为 ,TraceLevelVerbose
则示例会将 的名称myObject
和 category
输出到 。Listeners 有关将侦听器添加到集合的信息 Listeners ,请参阅 TraceListenerCollection 类。
然后,如果 TraceLevel 设置为 Error
或更高,则示例在与第一条消息相同的行上输出第二条错误消息。 第二条消息后跟行终止符。
// Class-level declaration.
// Create a TraceSwitch.
private:
static TraceSwitch^ generalSwitch =
gcnew TraceSwitch( "General", "Entire Application" );
public:
static void MyErrorMethod( Object^ myObject, String^ category )
{
#if defined(TRACE)
// Write the message if the TraceSwitch level is set to Verbose.
Trace::WriteIf( generalSwitch->TraceVerbose, myObject, category );
// Write a second message if the TraceSwitch level is set to
// Error or higher.
Trace::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.
Trace.WriteIf(generalSwitch.TraceVerbose, myObject, category);
// Write a second message if the TraceSwitch level is set to Error or higher.
Trace.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.
Trace.WriteIf(generalSwitch.TraceVerbose, myObject, category)
' Write a second message if the TraceSwitch level is set to Error or higher.
Trace.WriteLineIf(generalSwitch.TraceError, _
" Object is not valid for this category.")
End Sub
注解
默认情况下,输出将写入 实例 DefaultTraceListener。
参数 category
可用于对输出消息进行分组。
此方法调用 Write 跟踪侦听器的 方法。
继承者说明
可以使用 语句而不是 WriteIf(Boolean, String) using 语句来最大程度地降低检测应用程序If...Then
的性能损失。 以下两个代码示例发送相同的调试消息。 但是,当跟踪关闭时,第一个示例要快得多,因为如果 mySwitch.TraceError
计算结果为 false
,则不会调用 Write(String)。 第二个示例始终调用 WriteIf(Boolean, String),即使 为 false
mySwitch.TraceError
且未生成跟踪输出。 这可能会导致不必要地执行任意复杂的代码。
第一个示例
if(mySwitch.TraceError)
Trace.Write("aNumber = " + aNumber + " out of range");
第二个示例
Trace.WriteIf(mySwitch.TraceError, "aNumber = " + aNumber + " out of range");
另请参阅
适用于
WriteIf(Boolean, Object)
- Source:
- Trace.cs
- Source:
- Trace.cs
- Source:
- Trace.cs
如果条件为 true
,则向 Listeners 集合中的跟踪侦听器写入对象的 ToString() 方法。
public:
static void WriteIf(bool condition, System::Object ^ value);
[System.Diagnostics.Conditional("TRACE")]
public static void WriteIf (bool condition, object? value);
[System.Diagnostics.Conditional("TRACE")]
public static void WriteIf (bool condition, object value);
[<System.Diagnostics.Conditional("TRACE")>]
static member WriteIf : bool * obj -> unit
Public Shared Sub WriteIf (condition As Boolean, value As Object)
参数
- condition
- Boolean
若要使消息被写入,则为 true
;否则为 false
。
- 属性
示例
以下示例创建一个名为 TraceSwitch 的 generalSwitch
。 此开关在代码示例外部设置。
如果开关设置为 TraceLevelError
或更高版本,则示例会将值参数的名称输出到 Listeners。 有关将侦听器添加到集合的信息 Listeners ,请参阅 TraceListenerCollection 类。
然后,如果 TraceLevel 设置为 Verbose
,则示例在与第一条消息相同的行上输出消息。 第二条消息后跟行终止符。
// Class-level declaration.
// Create a TraceSwitch.
private:
static TraceSwitch^ generalSwitch =
gcnew TraceSwitch( "General", "Entire Application" );
public:
static void MyErrorMethod( Object^ myObject )
{
#if defined(TRACE)
// Write the message if the TraceSwitch level is set
// to Error or higher.
Trace::WriteIf( generalSwitch->TraceError, myObject );
// Write a second message if the TraceSwitch level is set
// to Verbose.
Trace::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.
Trace.WriteIf(generalSwitch.TraceError, myObject);
// Write a second message if the TraceSwitch level is set to Verbose.
Trace.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.
Trace.WriteIf(generalSwitch.TraceError, myObject)
' Write a second message if the TraceSwitch level is set to Verbose.
Trace.WriteLineIf(generalSwitch.TraceVerbose, _
" is not a valid value for this method.")
End Sub
注解
默认情况下,输出将写入 实例 DefaultTraceListener。
此方法调用 Write 跟踪侦听器的 方法。
继承者说明
可以使用 语句而不是 WriteIf(Boolean, String) using 语句来最大程度地降低检测应用程序If...Then
的性能损失。 以下两个代码示例发送相同的调试消息。 但是,当跟踪关闭时,第一个示例要快得多,因为如果 mySwitch.TraceError
计算结果为 false
,则不会调用 Write(String)。 第二个示例始终调用 WriteIf(Boolean, String),即使 为 false
mySwitch.TraceError
且未生成跟踪输出。 这可能会导致不必要地执行任意复杂的代码。
第一个示例
if(mySwitch.TraceError)
Trace.Write("aNumber = " + aNumber + " out of range");
第二个示例
Trace.WriteIf(mySwitch.TraceError, "aNumber = " + aNumber + " out of range");
另请参阅
适用于
WriteIf(Boolean, String)
- Source:
- Trace.cs
- Source:
- Trace.cs
- Source:
- Trace.cs
如果条件为 true
,则将消息写入 Listeners 集合中的跟踪侦听器。
public:
static void WriteIf(bool condition, System::String ^ message);
[System.Diagnostics.Conditional("TRACE")]
public static void WriteIf (bool condition, string? message);
[System.Diagnostics.Conditional("TRACE")]
public static void WriteIf (bool condition, string message);
[<System.Diagnostics.Conditional("TRACE")>]
static member WriteIf : bool * string -> unit
Public Shared Sub WriteIf (condition As Boolean, message As String)
参数
- condition
- Boolean
若要使消息被写入,则为 true
;否则为 false
。
- message
- String
要写入的消息。
- 属性
示例
以下示例创建一个名为 TraceSwitch 的 generalSwitch
。 此开关在代码示例外部设置。
如果开关设置为 TraceLevelError
或更高版本,则示例会将第一条错误消息输出到 Listeners。 有关将侦听器添加到集合的信息 Listeners ,请参阅 TraceListenerCollection 类。
然后,如果 TraceLevel 设置为 Verbose
,则示例在与第一条消息相同的行上输出第二条错误消息。 第二条消息后跟行终止符。
// Class-level declaration.
// Create a TraceSwitch.
private:
static TraceSwitch^ generalSwitch =
gcnew TraceSwitch( "General", "Entire Application" );
public:
static void MyErrorMethod()
{
#if defined(TRACE)
// Write the message if the TraceSwitch level is set to
// Error or higher.
Trace::WriteIf( generalSwitch->TraceError, "My error message. " );
// Write a second message if the TraceSwitch level is set
// to Verbose.
Trace::WriteLineIf( generalSwitch->TraceVerbose,
"My second error message." );
#endif
}
// Class-level declaration.
// Create a TraceSwitch.
static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application");
static public void MyErrorMethod() {
// Write the message if the TraceSwitch level is set to Error or higher.
Trace.WriteIf(generalSwitch.TraceError, "My error message. ");
// Write a second message if the TraceSwitch level is set to Verbose.
Trace.WriteLineIf(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.
Trace.WriteIf(generalSwitch.TraceError, "My error message. ")
' Write a second message if the TraceSwitch level is set to Verbose.
Trace.WriteLineIf(generalSwitch.TraceVerbose, "My second error message.")
End Sub
注解
默认情况下,输出将写入 实例 DefaultTraceListener。
此方法调用 Write 跟踪侦听器的 方法。
继承者说明
可以使用 语句而不是 WriteIf(Boolean, String) using 语句来最大程度地降低检测应用程序If...Then
的性能损失。 以下两个代码示例发送相同的调试消息。 但是,当跟踪关闭时,第一个示例要快得多,因为如果 mySwitch.TraceError
计算结果为 false
,则不会调用 Write(String)。 第二个示例始终调用 WriteIf(Boolean, String),即使 为 false
mySwitch.TraceError
且未生成跟踪输出。 这可能会导致不必要地执行任意复杂的代码。
第一个示例
if(mySwitch.TraceError)
Trace.Write("aNumber = " + aNumber + " out of range");
第二个示例
Trace.WriteIf(mySwitch.TraceError, "aNumber = " + aNumber + " out of range");
另请参阅
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈