Yesterday I was complaining to Kieran about how hard it was to debug a custom task. When working on my AssemblyInfo task I was debugging all my regular expressions by running them in a separate command-line app, and with Log.LogMessage() calls in the task.

Kieran listened politely to my complaining, but clearly thought I was an idiot. He then proceeded to show me how to debug a custom task within Visual Studio. Here’s how to do it:

  1. Create a test project to run your task in
  2. Edit the .csproj/.vbproj file and add in the necessary MSBuild XML to make use of your task. Here is what I added:

    <UsingTask AssemblyFile="D:\tasks\assemblyinfotask.dll" TaskName="AssemblyInfo"/>
        <Target Name="BeforeBuild" Outputs="@(AssemblyInfo)">
        <AssemblyInfo AssemblyInfoFile="@(AssemblyInfo)"/>

  3. Close the test project
  4. Open your custom task project
  5. Go to Project > projectname Properties…
  6. Go to the Debug tab
  7. Change the start action to “Start external program” and put in the full path to MSBuild as the program. In my case the path is “E:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe”
  8. Change the “Command line arguments” to the full path to your test project
  9. Close the property window

Voila! Now you can set a breakpoint on the Execute() method in your custom task and single-step through all your code. Thanks Kieran!

[ Author: Neil Enns ]