Debugging Templates

CodeSmith Generator supports debugging by using the CLR's Just-in-Time debugger.  This article will show some tips and tricks in setting up CodeSmith Generator 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 a System.Diagnostics.Debugger.Break() statement. If you are using a Code Behind, please remember to import the System.Diagnostics namespace.


You must call System.Diagnostics.Debugger.Launch(); before your first System.Diagnostics.Debugger.Break() Statement or the process will crash.

Using the Debugger

When you execute a template and it encounters a break point, you will see the following dialog.

When you see this dialog choose the debugger you want to use and click on the Yes button.

You can now debug a template just how you would debug any .NET project.

Debugging in Windows Vista or Windows 7

There are some extra steps that need to be completed before using the Just-In-Time debugger in Windows Vista or Windows 7. 

First you need to make sure you have all the latest service packs installed. Next, the debugger in Vista will cause CodeSmith Generator 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_MACHINE\SOFTWARE\Microsoft\.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_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.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 Generator when you continue the execution of the template from the debugger.

If you are running into issues when trying to debug, run CodeSmith Generator 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 Generator with full administrator rights by right clicking and choose run as administrator.