[MsBuild] Writing and Debugging Custom Tasks

Writing a custom task for MSBuild is very simple, here's the bare minimum code you would need to get you going:

using System;

using System.Collections.Generic;

using System.Text;


using Microsoft.Build.Framework;

using Microsoft.Build.Utilities;


namespace IRM.Tasks


    public class HelloTask : Task


        private string name;



        public string Name


            get { return name; }

            set { name = value; }



        public override bool Execute()


            Log.LogMessage("Hello " + name);

            return true;





The task will obviously print out the value of the "Name" property to the log. 

To test it, create a test-project like a dummy console app and add this to the end of the .proj file:

<UsingTask TaskName="IRM.Tasks.HelloTask"    AssemblyFile="C:\code\HelloTask\bin\Debug\IRM.Tasks.dll" />


<Target Name="AfterBuild">

      <HelloTask Name="Johan" />


If you unload the project from the Solution Explorer window you can Edit it directly in VS. 

But if you want to debug the task and set breakpoints in it? It was described by Neil Enns of the MSBuild team quite some time ago. After you have created a test-project and added the custom task to the .proj file as described above, do this:

  1. Open your custom task project
  2. Go to Project > projectname Properties…
  3. Go to the Debug tab
  4. 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 “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe”
  5. Change the “Command line arguments” to the full path to your test project
  6. Close the property window

Now just start debugging your custom task, MSBuild should start up and start to build your test-project and will stop at any breakpoints you set in your custom task code. Neat!

No Comments