Outputting Trace and Debug Information

One useful way to gather debugging information when a template is not behaving as you expect is to use the methods of the .NET Trace and Debug objects. These objects let your code interact with the Debug pane of the CodeSmith Studio [Output window|http://docs.codesmithtools.com/display/Generator/The+Output+Window]. The two objects have exactly the same members; the only difference between the two is that the Trace object is active at all times, while the Debug object is only active when you compile your code in [debug mode|http://docs.codesmithtools.com/display/Generator/Compiling+in+Debug+Mode].

This table summarizes some of the useful members of the Trace and Debug objects:

Member

Type

Description

Assert

Method

Checks for a condition, and displays a message if the condition is false.

Fail

Method

Displays an error message

Indent

Method

Increases the current IndentLevel by one.

IndentLevel

Property

Specifies the indent level.

IndentSize

Property

Specifies the number of spaces in an indent.

Unindent

Method

Decreases the current IndentLevel by one.

Write

Method

Writes the given information

WriteIf

Method

Writes the given information only if a condition is true

WriteLine

Method

Same as Write but appends a new line character after the information.

WriteLineIf

Method

Same as WriteIf but appends a new line character after the information.