Debug.WriteIf 方法

定义

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

重载

WriteIf(Boolean, Debug+WriteIfInterpolatedStringHandler)

如果 conditiontrue,则向集合中的 Listeners 跟踪侦听器写入类别名称和消息。

WriteIf(Boolean, Object)

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

WriteIf(Boolean, String)

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

WriteIf(Boolean, Debug+WriteIfInterpolatedStringHandler, String)

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

WriteIf(Boolean, Object, String)

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

WriteIf(Boolean, String, String)

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

WriteIf(Boolean, Debug+WriteIfInterpolatedStringHandler)

Source:
Debug.cs
Source:
Debug.cs
Source:
Debug.cs

如果 conditiontrue,则向集合中的 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,则将值写入集合中的跟踪侦听器。

message
Debug.WriteIfInterpolatedStringHandler

要写入的消息(如果 conditiontrue)。

属性

注解

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

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

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

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

另请参阅

适用于

WriteIf(Boolean, Object)

Source:
Debug.cs
Source:
Debug.cs
Source:
Debug.cs

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

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,则将值写入集合中的跟踪侦听器。

value
Object

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

属性

示例

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

如果开关设置为 TraceLevelError 或更高版本,则示例会将值参数的名称输出到 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 跟踪侦听器的 方法。

继承者说明

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

第一个示例:

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

第二个示例:

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

另请参阅

适用于

WriteIf(Boolean, String)

Source:
Debug.cs
Source:
Debug.cs
Source:
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

要写入的消息。

属性

示例

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

如果开关设置为 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 跟踪侦听器的 方法。

继承者说明

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

第一个示例:

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

第二个示例:

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

另请参阅

适用于

WriteIf(Boolean, Debug+WriteIfInterpolatedStringHandler, String)

Source:
Debug.cs
Source:
Debug.cs
Source:
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,则将消息写入集合中的跟踪侦听器。

message
Debug.WriteIfInterpolatedStringHandler

要写入的消息。

category
String

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

属性

注解

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

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

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

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

另请参阅

适用于

WriteIf(Boolean, Object, String)

Source:
Debug.cs
Source:
Debug.cs
Source:
Debug.cs

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

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,则将类别名称和值写入集合中的跟踪侦听器。

value
Object

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

category
String

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

属性

示例

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

如果开关设置为 ,TraceLevelVerbose则示例会将 的名称myObjectcategory 输出到 。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 跟踪侦听器的 方法。

继承者说明

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

第一个示例:

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

第二个示例:

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

另请参阅

适用于

WriteIf(Boolean, String, String)

Source:
Debug.cs
Source:
Debug.cs
Source:
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

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

属性

示例

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

如果 开关设置为 , 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 跟踪侦听器的 方法。

继承者说明

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

第一个示例:

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

第二个示例:

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

另请参阅

适用于