How to write console output from a custom MSBuild task?

I am trying to debug an MSBuild task, and I know that there is a way to log MSBuild from a user task, but I forgot how to do it.

+5
source share
2 answers

The Task base has a property Logthat you can use:

Log.LogMessage("My message");
+7
source

In order to test the modules, I wrap the log around a helper class

public static void Log(ITask task, string message, MessageImportance importance)
{
    try
    {
        BuildMessageEventArgs args = new BuildMessageEventArgs(message, string.Empty, 
            task.ToString(), importance);
        task.BuildEngine.LogMessageEvent(args);
    }
    catch (NullReferenceException)
    {
        // Don't throw as task and BuildEngine will be null in unit test.
    }
}

Currently, I would probably convert this to an extension method for convenience.

+1
source

All Articles