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.
Leave a Reply