— New online course!  Getting Started with Azure DevOps — Special launch price 50% off —

Tip: Make TFS2012 build logging easier using extension methods in Microsoft.TeamFoundation.Build.Workflow.Activities

by

I’ve been writing a lot of custom Workflow Activities for Team Foundation Server 2012 (TFS2012) Builds lately.  A custom Workflow Activity lets you drop application-specific logic and steps into theout-of-the-box TFS build scripts (aka. “build process templates”).  When you write one of these Activities, you almost always need/want to write to the TFS build log when the build runs. 

Turns out that I’ve been writing to the log the hard way.  I had my own utility method that would create a BuildMessage object, populate it, and then call Track() on the CodeActivityContext to put that message into the build script.  It worked but it wasn’t elegant. 

public sealed class LogUtility
{
    public static void WriteToLog(
        CodeActivityContext context,
        string message,
        BuildMessageImportance importance)
    {
        var buildInfo =
            new BuildInformationRecord<BuildMessage>();

        BuildMessage newBuildMessage = new BuildMessage();

        newBuildMessage.Importance = importance;
        newBuildMessage.Message = message;

        buildInfo.Value = newBuildMessage;

        context.Track(buildInfo);  
    }       
}

The real, right, and easy way to write to the log is to use the extension methods that are in Microsoft.TeamFoundation.Build.Workflow.Activities.  Drop in a using statement for Microsoft.TeamFoundation.Build.Workflow.Activities and VOILA! the CodeActivityContext object now how TrackBuildMessage(), TrackBuildWarning(), and TrackBuildError()

using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using Microsoft.TeamFoundation.Build.Client;

// this is the magic using statement
// that makes build logging much easier
using Microsoft.TeamFoundation.Build.Workflow.Activities;

namespace Benday.TeamBuild2012.Activities
{
    [BuildActivity(HostEnvironmentOption.Agent)]    public sealed class MyCustomActivity : CodeActivity
    {
        protected override void Execute(
             CodeActivityContext context)
        {
            context.TrackBuildMessage(“This is a message.”);
            context.TrackBuildWarning(“This is a warning.”);
            context.TrackBuildError(“This is an error.”);
        }
    }
}

-Ben

 

— Want to start doing automated builds but you’re not sure where or how to start?  Need help customizing your build process?  Dreaming of a better life with a streamlined development-to-testing flow using Team Foundation Server Lab Management?  Drop us a line at info@benday.com


2 Responses to "Tip: Make TFS2012 build logging easier using extension methods in Microsoft.TeamFoundation.Build.Workflow.Activities"
  1. Hello,

    thanks for this article and the whole site. Is very usefull.

    I have a problem with a custom activity I wrote. If I don’t put any error message, I can’t see the info and warning message I have in the custom activity. If I put an error message, then I could see all the messages.

    What can I do to see any message without putting any error message?

    Thank you very much!

    • Hi Vicenc,

      Have you resolved your issue? I have similar issue with you that only TrackBuildWarning and TrackBuildError print the log in build. TrackBuildMessage prints nothing, even I set BuildMessageImportance.High

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: