5

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.

2 Answers 2

9

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

Log.LogMessage("My message");
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.