Debugging templates
CodeSmith Generator templates are compiled .NET code, so you can step through them with the Just-In-Time debugger just like any other .NET code.
Debugging a template
-
Set the
Debugattribute of the CodeTemplate directive toTrue, so that debug symbols are included when the template is compiled:<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="True" %> -
Where you want execution to stop, call
System.Diagnostics.Debugger.Launch()and thenSystem.Diagnostics.Debugger.Break():System.Diagnostics.Debugger.Launch(); System.Diagnostics.Debugger.Break();If you use the short names
Debugger.Launch()andDebugger.Break()in a code-behind file, import theSystem.Diagnosticsnamespace. -
Generate the template. When it reaches the breakpoint, the Visual Studio Just-In-Time Debugger dialog asks which debugger to use. Choose one and click Yes.
You can now debug the template as you would any .NET project: inspect locals, view the call stack and step through the code.
Important
Always call Debugger.Launch() before your first Debugger.Break(). Otherwise the process crashes.
If Generator stops responding after debugging
If Generator stops responding when you finish debugging, change the Just-In-Time debugger's DbgJITDebugLaunchSetting registry value to 2 under this key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework
On 64-bit Windows, also set DbgJITDebugLaunchSetting to 2 under:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework
With this setting, the debugger dialog appears as soon as your code hits a breakpoint, and control returns to Generator when you continue running the template from the debugger.
Troubleshooting
- If you have trouble debugging, run both Generator and Visual Studio as an administrator.
- If Visual Studio says "There is no source code available for the current location", make Source Code (Text) Editor the default editor for
.cstfiles in Visual Studio.
More debugging tools
| Page | What you learn |
|---|---|
| Outputting trace and debug information | Write diagnostic messages to the Output window with Trace and Debug. |
| Viewing the compiled template source code | See the C# or VB code that Generator compiles from your template. |