CodeSmith Community
Your Code. Your Way. Faster!
Tips & Tricks: Debugging CodeSmith

Overview

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.

Debugger.Break();

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.

System.Diagnostics.Debugger.Break();

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_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 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.

Troubleshooting

  • 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.

Posted 06-09-2007 11:03 PM by shannon
Filed under: ,

Comments

Trial wrote Learn insider tips and tricks from the developers of CodeSmith!
on 12-08-2008 3:20 PM

Subscribe to our Tips & Tricks blog to get updates of new Tips & Tricks that will help you get

Blog » Archive » Debug a CodeSmith template wrote Blog &raquo; Archive &raquo; Debug a CodeSmith template
on 01-15-2009 2:47 PM

Pingback from  Blog  » Archive   » Debug a CodeSmith template

Blake Niemyjski wrote Tips & Tricks: Debugging CodeSmith on Microsoft Windows 7
on 06-03-2009 5:48 AM

We have updated the Tips & Tricks: Debugging CodeSmith with the changes needed to debug on Windows7

Blake Niemyjski wrote Tips & Tricks: Debugging CodeSmith on Microsoft Windows 7
on 06-03-2009 6:45 AM

We have updated the Tips & Tricks: Debugging CodeSmith with the changes needed to debug on Windows7

Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems