如何:向应用程序代码添加跟踪语句How to: Add Trace Statements to Application Code

跟踪使用最常用的方法是将输出写入侦听器的方法:WriteWriteIfWriteLineWriteLineIfAssertFailThe methods used most often for tracing are the methods for writing output to listeners: Write, WriteIf, WriteLine, WriteLineIf, Assert, and Fail. 这些方法可以分为两个类别:以无条件的形式写入WriteLine失败所有发出输出,而WriteIfWriteLineIfAssert则测试布尔条件,并根据条件的值来写入或不写入。These methods can be divided into two categories: Write, WriteLine, and Fail all emit output unconditionally, whereas WriteIf, WriteLineIf, and Assert test a Boolean condition, and write or do not write based on the value of the condition. WriteIf 和 WriteLineIf 在条件为 true 时发出输出,而 Assert 在条件为 false 时发出输出。WriteIf and WriteLineIf emit output if the condition is true, and Assert emits output if the condition is false.

当设计跟踪和调试策略时,应考虑所需的输出形式。When designing your tracing and debugging strategy, you should think about how you want the output to look. 填充不相关信息的多个 Write 语句将创建难以读取的日志。Multiple Write statements filled with unrelated information will create a log that is difficult to read. 另一方面,如果使用 WriteLine 将相关语句放置在单独的行上,可能会难以区分哪些信息应该在一起。On the other hand, using WriteLine to put related statements on separate lines may make it difficult to distinguish what information belongs together. 通常,当需要将来自多个源的信息组合起来创建单个信息性消息时,使用多个 Write;当需要创建单个完整消息时,则使用 WriteLine 语句。In general, use multiple Write statements when you want to combine information from multiple sources to create a single informative message, and use the WriteLine statement when you want to create a single, complete message.

写入完整的行To write a complete line

  1. 调用 WriteLineWriteLineIf 方法。Call the WriteLine or WriteLineIf method.

    一个回车符会被追加到此方法返回的消息末尾,使 Write、WriteIf、WriteLine 或 WriteLineIf 返回的下一条消息将从以下行开始:A carriage return is appended to the end of the message this method returns, so that the next message returned by Write, WriteIf, WriteLine, or WriteLineIf will begin on the following line:

    Dim errorFlag As Boolean = False  
    Trace.WriteLine("Error in AppendData procedure.")  
    Trace.WriteLineIf(errorFlag, "Error in AppendData procedure.")  
    
    bool errorFlag = false;  
    System.Diagnostics.Trace.WriteLine ("Error in AppendData procedure.");  
    System.Diagnostics.Trace.WriteLineIf(errorFlag,   
       "Error in AppendData procedure.");  
    

写入部分行To write a partial line

  1. 调用 WriteWriteIf 方法。Call the Write or WriteIf method.

    由 Write、WriteIf、WriteLine 或 WriteLineIf 生成的下一条消息将从由 Write 或 WriteIf 语句生成的消息所在的同一行上开始:The next message put out by a Write, WriteIf, WriteLine, or WriteLineIf will begin on the same line as the message put out by the Write or WriteIf statement:

    Dim errorFlag As Boolean = False  
    Trace.WriteIf(errorFlag, "Error in AppendData procedure.")  
    Debug.WriteIf(errorFlag, "Transaction abandoned.")  
    Trace.Write("Invalid value for data request")  
    
    bool errorFlag = false;  
    System.Diagnostics.Trace.WriteIf(errorFlag,   
       "Error in AppendData procedure.");  
    System.Diagnostics.Debug.WriteIf(errorFlag, "Transaction abandoned.");  
    Trace.Write("Invalid value for data request");  
    

验证特定条件在执行方法之前或之后存在To verify that certain conditions exist either before or after you execute a method

  1. 调用 Assert 方法。Call the Assert method.

    Dim i As Integer = 4  
    Trace.Assert(i = 5, "i is not equal to 5.")  
    
    int i = 4;  
    System.Diagnostics.Trace.Assert(i == 5, "i is not equal to 5.");  
    

    备注

    可以将 Assert 用于跟踪和调试。You can use Assert with both tracing and debugging. 此示例将调用堆栈输出到 Listeners 集合中的任意侦听器。This example outputs the call stack to any listener in the Listeners collection. 有关详细信息,请参阅托管代码中的断言Debug.Assert和 。For more information, see Assertions in Managed Code and Debug.Assert.

请参阅See also