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.
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" %>
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();
When you execute a template in CodeSmith Studio and it encounters a break point, you will see the following dialogs.
When you see the user-defined breakpoint dialog, choose Debug.
Choose the Debugger you want to use.
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.
Subscribe to our Tips & Tricks blog to get updates of new Tips & Tricks that will help you get
Pingback from Blog » Archive » Debug a CodeSmith template
We have updated the Tips & Tricks: Debugging CodeSmith with the changes needed to debug on Windows7