Permalink
Fetching contributors…
Cannot retrieve contributors at this time
102 lines (84 sloc) 5.01 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Add Trace Statements to Application Code
03/30/2017
.net-framework
dotnet-clr
article
VB
CSharp
C++
jsharp
tracing [.NET Framework], conditional writes based on switches
trace statements
WriteLineIf method
tracing [.NET Framework], adding trace statements
Assert method, tracing code
trace switches, conditional writes based on switches
WriteIf method
f3a93fa7-1717-467d-aaff-393e5c9828b4
13
mairaw
mairaw
wpickett

How to: Add Trace Statements to Application Code

The methods used most often for tracing are the methods for writing output to listeners: Write, WriteIf, WriteLine, WriteLineIf, Assert, and Fail. 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 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. Multiple Write statements filled with unrelated information will create a log that is difficult to read. On the other hand, using WriteLine to put related statements on separate lines may make it difficult to distinguish what information belongs together. 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. Call the xref:System.Diagnostics.Trace.WriteLine%2A or xref:System.Diagnostics.Trace.WriteLineIf%2A method.

    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. Call the xref:System.Diagnostics.Trace.Write%2A or xref:System.Diagnostics.Trace.WriteIf%2A method.

    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. Call the xref:System.Diagnostics.Trace.Assert%2A 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.");  

    [!NOTE] You can use Assert with both tracing and debugging. This example outputs the call stack to any listener in the Listeners collection. For more information, see Assertions in Managed Code and xref:System.Diagnostics.Debug.Assert%2A?displayProperty=nameWithType.

See Also

xref:System.Diagnostics.Debug.WriteIf%2A?displayProperty=nameWithType
xref:System.Diagnostics.Debug.WriteLineIf%2A?displayProperty=nameWithType
xref:System.Diagnostics.Trace.WriteIf%2A?displayProperty=nameWithType
xref:System.Diagnostics.Trace.WriteLineIf%2A?displayProperty=nameWithType
Tracing and Instrumenting Applications
How to: Create, Initialize and Configure Trace Switches
Trace Switches
Trace Listeners