Confusing behavior of MSBuild logger on raising LogError event


Today I found that raising the error message was not enough for the msbuild to stop and invoke the targets specified by OnError tag. For example in the given sample -   


Custom task:- MSBuildTask.dll


namespace MSBuildTask


    public class MSBuildTask : Task


        public override bool Execute()



            string msg = " message before raising error event";


new BuildMessageEventArgs( msg, String.Empty,

String.Empty, MessageImportance.Normal));


            msg = " error message ";


                  new BuildErrorEventArgs(String.Empty, String.Empty,

String.Empty, 0, 0, 0, 0, msg, String.Empty,



            msg = "error message after error event";


               new BuildMessageEventArgs(msg, String.Empty,

                  String.Empty, MessageImportance.Normal));               


            return true;






Project file




<UsingTask TaskName="MSBuildTask.MSBuildTask"

      AssemblyFile="MSBuildTask.dll" />  


  <Target Name="TestTarget">

       <Message Text=" Before starting the custom task "/> 

       <MSBuildTask ContinueOnError="true"/>      

       <Message Text=" After running the custom task "/>


       <OnError ExecuteTargets="TargetExecOnError;" />


  <Target Name="TargetExecOnError">

     <Message Text=" Task to be executed on error "/> 




Irrespective of whether you set ContinueOnError to true or false, you will never be able to execute targets specified by OnError tag. Moreover you will get the message “error message after error eventon console. This is contrary to believe I had that raising error event in MSBuild is equivalent to raising an exception in code and the rest of task code/following targets will not execute. This is not true.



  1. The targets specified by OnError tag will be executed only when task returns false. If you are raising the error event in your custom msbuild task then it is your responsibility to return false status and make task fails.

  2. Raising the error event is not equivalent to raising the exception. As shown in the example above, you will always get the message “After running the custom task”


Comments (3)

  1. James Manning gives us some ideas of what to do when someone takes a file hostage with a lock in their…

  2. Mitch says:

    Does this work as you expect if you put OnError as the first statement in the target (as the docs state)?

  3. Ben says:

    This works as expected to me, your task always returns true so why would it execute the OnError target?

    When you say you "raising the error message" you should say "log the error message".  LogErroEvent doesn’t raise anything, it just writes a log entry.

Skip to main content