Tips & Tricks: Debugging CodeSmith

For the most up-to-date version of this article please see the following documentation.


CodeSmith supports debugging by using the CLR’s Just-in-Time debugger. This article will show some tips and tricks in setting up CodeSmith templates to use the debugger.

Allow Debugging in Template

The first step to allow debugging a template is to set the Debug attribute on the CodeTemplate declarative to true.

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="True" %>

Setting a Break Point

In order to get the Just-in-Time debugger to load and stop at a point in your code, you need to use the Debugger.Break() statement.


If you are setting a break point in a code behind file or included class, you need to include the namespace for the debugger class.


Using the Debugger

When you execute a template in CodeSmith Studio and it encounters a break point, you will see the following dialogs.

User Defined Break Point
When you see the user-defined breakpoint dialog, choose Debug.

Just-In-Time Debugger
Choose the Debugger you want to use.

Microsoft CLR Debugger

Debugging in Windows Vista or Windows 7

There are some problems with using the Just-In-Time debugger in Windows Vista and Windows 7.

First you need to make sure you have all the latest service packs installed. Next, the debugger in Vista will cause CodeSmith to hang when you finish debugging. You can work around this issue by updating the Just-In-Time debugger setting DbgJITDebugLaunchSetting. The setting is found in the registry at [HKEY_LOCAL_MACHINESOFTWAREMicrosoft.NETFramework]. Change the value of DbgJITDebugLaunchSetting to 2. If you are using a 64bit operating system then you must also set the same key (DbgJITDebugLaunchSetting) in this folder [HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoft.NETFramework] to 2. This will cause the debugger dialog to be displayed immediately when your code hits a breakpoint. This will also allow control to return to CodeSmith when you continue the execution of the template from the debugger.

Note: If you are running into issues when trying to debug, run CodeSmith and Visual Studio as an Administrator.


  • If you are having trouble with the debugger, try using the CLR debugger as that tends to work better.
  • If you are getting the message, "There is no source code available for the current location.", you need to change the default editor for .cst files in Visual Studio to be the "Source Code (Text) Editor".
  • If you are having further issues in Vista, make sure to run CodeSmith with full administrator rights by right clicking and choose run as administrator.