Question

I'm trying to debug an MSBuild task, and I know there is some way to write to the MSBuild log from within a custom task but I forget how.

Was it helpful?

Solution

The base Task class has a Log property you can use:

Log.LogMessage("My message");

OTHER TIPS

For unit testing purposes, I wrap the logger 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.
    }
}

Nowadays I'd probably convert that into an extension method for convenience.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top