Debug.WriteLineIf 方法

定义

如果条件为 true,则将有关调试的信息写入 Listeners 集合中的跟踪侦听器。

重载

WriteLineIf(Boolean, Object, String)

如果条件为 true,则向 Listeners 集合中的跟踪侦听器写入类别名称和对象的 ToString() 方法值。

WriteLineIf(Boolean, Debug+WriteIfInterpolatedStringHandler)

如果指定条件为 true,则向集合中的Listeners跟踪侦听器写入消息。

WriteLineIf(Boolean, Object)

如果条件为 true,则向 Listeners 集合中的跟踪侦听器写入对象的 ToString() 方法。

WriteLineIf(Boolean, String)

如果条件为 true,则将消息写入 Listeners 集合中的跟踪侦听器。

WriteLineIf(Boolean, Debug+WriteIfInterpolatedStringHandler, String)

如果指定条件为 true,将类别名称和消息写入集合中的Listeners跟踪侦听器。

WriteLineIf(Boolean, String, String)

如果条件为 true,则将类别名称和消息写入 Listeners 集合中的跟踪侦听器。

WriteLineIf(Boolean, Object, String)

如果条件为 true,则向 Listeners 集合中的跟踪侦听器写入类别名称和对象的 ToString() 方法值。

public:
 static void WriteLineIf(bool condition, System::Object ^ value, System::String ^ category);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteLineIf (bool condition, object value, string category);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteLineIf (bool condition, object? value, string? category);
[<System.Diagnostics.Conditional("DEBUG")>]
static member WriteLineIf : bool * obj * string -> unit
Public Shared Sub WriteLineIf (condition As Boolean, value As Object, category As String)

参数

condition
Boolean

要计算的条件表达式。 如果条件为 true,则将类别名称和值写入集合中的跟踪侦听器。

value
Object

一个对象,其名称被发送到 Listeners

category
String

用于组织输出的类别名称。

属性

示例

以下示例创建一个 TraceSwitch 命名 generalSwitch的 。 此开关是在代码示例外部设置的。

如果开关设置为TraceLevelError或更高版本,示例会将第一条错误消息输出到 。Listeners 有关将侦听器添加到 Listeners 集合的信息,请参阅 TraceListenerCollection 类。

Then, if the TraceLevel is set to Verbose, the example outputs the second error message on the same line as the first message. 行终止符遵循第二条消息。

   // 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->TraceError, "Invalid object for category. ");
      
      // Write a second message if the TraceSwitch level is set to Verbose.
      Debug::WriteLineIf( generalSwitch->TraceVerbose, myObject, 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 Error or higher.
    Debug.WriteIf(generalSwitch.TraceError, "Invalid object for category. ");

    // Write a second message if the TraceSwitch level is set to Verbose.
    Debug.WriteLineIf(generalSwitch.TraceVerbose, myObject, 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 Error or higher.
    Debug.WriteIf(generalSwitch.TraceError, "Invalid object for category. ")
    
    ' Write a second message if the TraceSwitch level is set to Verbose.
    Debug.WriteLineIf(generalSwitch.TraceVerbose, myObject, category)
End Sub

注解

默认情况下,输出将写入到实例 DefaultTraceListener中。

category 参数可用于对输出消息进行分组。

此方法调用 WriteLine 跟踪侦听器的方法。

继承者说明

可以使用语句而不是使用WriteLineIf(Boolean, String)语句来最大程度地降低检测应用程序If...Then的性能损失。 以下两个代码示例发送相同的调试消息。 但是,跟踪关闭时,第一个示例要快得多,因为如果 mySwitch.TraceError 的计算结果 false为,则不会调用 WriteLine(String)。 第二个示例始终调用 WriteLineIf(Boolean, String),即使 mySwitch.TraceErrorfalse 且不会生成跟踪输出。 这可能会导致不必要的执行任意复杂代码。

第一个示例:

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

第二个示例:

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

另请参阅

适用于

WriteLineIf(Boolean, Debug+WriteIfInterpolatedStringHandler)

如果指定条件为 true,则向集合中的Listeners跟踪侦听器写入消息。

public:
 static void WriteLineIf(bool condition, System::Diagnostics::Debug::WriteIfInterpolatedStringHandler % message);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteLineIf (bool condition, ref System.Diagnostics.Debug.WriteIfInterpolatedStringHandler message);
[<System.Diagnostics.Conditional("DEBUG")>]
static member WriteLineIf : bool * WriteIfInterpolatedStringHandler -> unit
Public Shared Sub WriteLineIf (condition As Boolean, ByRef message As Debug.WriteIfInterpolatedStringHandler)

参数

condition
Boolean

要计算的条件表达式。 如果条件为 true,则将消息写入集合中的跟踪侦听器。

message
Debug.WriteIfInterpolatedStringHandler

要写入的消息。

属性

注解

.NET 6 中引入了此重载以提高性能。 与采用 String 参数的重载相比,此重载仅在需要消息时计算任何内插字符串格式项。

默认情况下,输出将写入到实例 DefaultTraceListener中。

此方法调用 TraceListener.WriteLine 跟踪侦听器的方法。

另请参阅

适用于

WriteLineIf(Boolean, Object)

如果条件为 true,则向 Listeners 集合中的跟踪侦听器写入对象的 ToString() 方法。

public:
 static void WriteLineIf(bool condition, System::Object ^ value);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteLineIf (bool condition, object value);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteLineIf (bool condition, object? value);
[<System.Diagnostics.Conditional("DEBUG")>]
static member WriteLineIf : bool * obj -> unit
Public Shared Sub WriteLineIf (condition As Boolean, value As Object)

参数

condition
Boolean

要计算的条件表达式。 如果条件为 true,则将值写入集合中的跟踪侦听器。

value
Object

一个对象,其名称被发送到 Listeners

属性

示例

以下示例创建一个 TraceSwitch 命名 generalSwitch的 。 此开关在代码示例外部设置。

如果开关设置为TraceLevelError或更高版本,则示例会将第一条错误消息输出到 。Listeners 有关将侦听器添加到 Listeners 集合的信息,请参阅 TraceListenerCollection 类。

然后,如果设置为 VerboseTraceLevel,该示例将输出与第一条消息相同的行上的对象名称。 行终止符遵循第二条消息。

   // 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, "Invalid object. " );
      
      // Write a second message if the TraceSwitch level is set to Verbose.
      Debug::WriteLineIf( generalSwitch->TraceVerbose, myObject );
      #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, "Invalid object. ");

    // Write a second message if the TraceSwitch level is set to Verbose.
    Debug.WriteLineIf(generalSwitch.TraceVerbose, myObject);
}
' 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

注解

默认情况下,输出将写入到实例 DefaultTraceListener中。

此方法调用 WriteLine 跟踪侦听器的方法。

继承者说明

可以使用语句而不是使用WriteLineIf(Boolean, String)语句来最大程度地降低检测应用程序If...Then的性能损失。 以下两个代码示例发送相同的调试消息。 但是,跟踪关闭时,第一个示例要快得多,因为如果 mySwitch.TraceError 的计算结果 false为,则不会调用 WriteLine(String)。 第二个示例始终调用 WriteLineIf(Boolean, String),即使 mySwitch.TraceErrorfalse 且不会生成跟踪输出。 这可能会导致不必要的执行任意复杂代码。

第一个示例:

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

第二个示例:

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

另请参阅

适用于

WriteLineIf(Boolean, String)

如果条件为 true,则将消息写入 Listeners 集合中的跟踪侦听器。

public:
 static void WriteLineIf(bool condition, System::String ^ message);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteLineIf (bool condition, string message);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteLineIf (bool condition, string? message);
[<System.Diagnostics.Conditional("DEBUG")>]
static member WriteLineIf : bool * string -> unit
Public Shared Sub WriteLineIf (condition As Boolean, message As String)

参数

condition
Boolean

要计算的条件表达式。 如果条件为 true,则将消息写入集合中的跟踪侦听器。

message
String

要写入的消息。

属性

示例

以下示例创建一个 TraceSwitch 命名 generalSwitch的 。 此开关是在代码示例外部设置的。

如果开关设置为TraceLevelError或更高版本,示例会将第一条错误消息输出到 。Listeners 有关将侦听器添加到 Listeners 集合的信息,请参阅 TraceListenerCollection 该类。

Then, if the TraceLevel is set to Verbose, the example outputs the second error message on the same line as the first message. 行终止符遵循第二条消息。

   // 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::WriteLineIf( 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.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.
    Debug.WriteIf(generalSwitch.TraceError, "My error message. ")
    
    ' Write a second message if the TraceSwitch level is set to Verbose.
    Debug.WriteLineIf(generalSwitch.TraceVerbose, "My second error message.")
End Sub

注解

默认情况下,输出将写入到实例 DefaultTraceListener中。

此方法调用 TraceListener.WriteLine 跟踪侦听器的方法。

继承者说明

可以使用语句而不是使用WriteLineIf(Boolean, String)语句来最大程度地降低检测应用程序If...Then的性能损失。 以下两个代码示例发送相同的调试消息。 但是,跟踪关闭时,第一个示例要快得多,因为如果 mySwitch.TraceError 的计算结果 false为,则不会调用 WriteLine(String)。 第二个示例始终调用 WriteLineIf(Boolean, String),即使 mySwitch.TraceErrorfalse 且不会生成跟踪输出。 这可能会导致不必要的执行任意复杂代码。

第一个示例:

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

第二个示例:

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

另请参阅

适用于

WriteLineIf(Boolean, Debug+WriteIfInterpolatedStringHandler, String)

如果指定条件为 true,将类别名称和消息写入集合中的Listeners跟踪侦听器。

public:
 static void WriteLineIf(bool condition, System::Diagnostics::Debug::WriteIfInterpolatedStringHandler % message, System::String ^ category);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteLineIf (bool condition, ref System.Diagnostics.Debug.WriteIfInterpolatedStringHandler message, string? category);
[<System.Diagnostics.Conditional("DEBUG")>]
static member WriteLineIf : bool * WriteIfInterpolatedStringHandler * string -> unit
Public Shared Sub WriteLineIf (condition As Boolean, ByRef message As Debug.WriteIfInterpolatedStringHandler, category As String)

参数

condition
Boolean

要计算的条件表达式。 如果条件为 true,则消息和类别名称将写入集合中的跟踪侦听器。

message
Debug.WriteIfInterpolatedStringHandler

要写入的消息。

category
String

用于组织输出的类别名称。

属性

注解

.NET 6 中引入了此重载以提高性能。 与采用 String 参数的重载相比,此重载仅在需要消息时计算任何内插字符串格式项。

默认情况下,输出将写入到实例 DefaultTraceListener中。

category 参数可用于对输出消息进行分组。

此方法调用 TraceListener.WriteLine 跟踪侦听器的方法。

另请参阅

适用于

WriteLineIf(Boolean, String, String)

如果条件为 true,则将类别名称和消息写入 Listeners 集合中的跟踪侦听器。

public:
 static void WriteLineIf(bool condition, System::String ^ message, System::String ^ category);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteLineIf (bool condition, string message, string category);
[System.Diagnostics.Conditional("DEBUG")]
public static void WriteLineIf (bool condition, string? message, string? category);
[<System.Diagnostics.Conditional("DEBUG")>]
static member WriteLineIf : bool * string * string -> unit
Public Shared Sub WriteLineIf (condition As Boolean, message As String, category As String)

参数

condition
Boolean

要计算的条件表达式。 如果条件为 true,则消息和 cateogry 名称将写入集合中的跟踪侦听器。

message
String

要写入的消息。

category
String

用于组织输出的类别名称。

属性

示例

以下示例创建一个 TraceSwitch 命名 generalSwitch的 。 此开关是在代码示例外部设置的。

如果开关设置为TraceLevelError或更高版本,示例会将第一条错误消息输出到 。Listeners 有关将侦听器添加到 Listeners 集合的信息,请参阅 TraceListenerCollection 类。

Then, if the TraceLevel is set to Verbose, the example outputs the second error message and the category on the same line as the first message. 行终止符遵循第二条消息。

   // Class-level declaration.
   // Create a TraceSwitch.
   static TraceSwitch^ generalSwitch = 
      gcnew TraceSwitch( "General","Entire Application" );

public:
   static void MyErrorMethod( String^ category )
   {
      // 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::WriteLineIf( generalSwitch->TraceVerbose, 
         "My second error message.", category );
      #endif
   }
// Class-level declaration.
// Create a TraceSwitch.
static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application");

static public void MyErrorMethod(string category)
{
    // 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.WriteLineIf(generalSwitch.TraceVerbose, "My second error message.", category);
}
' Class-level declaration.
' Create a TraceSwitch.
Private Shared generalSwitch As New TraceSwitch("General", "Entire Application")


Public Shared Sub MyErrorMethod(category As String)
    ' 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.WriteLineIf(generalSwitch.TraceVerbose, "My second error message.", category)
End Sub

注解

默认情况下,输出将写入到实例 DefaultTraceListener中。

category 参数可用于对输出消息进行分组。

此方法调用 TraceListener.WriteLine 跟踪侦听器的方法。

继承者说明

可以使用语句而不是使用WriteLineIf(Boolean, String)语句来最大程度地降低检测应用程序If...Then的性能损失。 以下两个代码示例发送相同的调试消息。 但是,跟踪关闭时,第一个示例要快得多,因为如果 mySwitch.TraceError 的计算结果 false为,则不会调用 WriteLine(String)。 第二个示例始终调用 WriteLineIf(Boolean, String),即使 mySwitch.TraceErrorfalse 且不会生成跟踪输出。 这可能会导致不必要的执行任意复杂代码。

第一个示例:

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

第二个示例:

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

另请参阅

适用于