Logging in Custom Build action for Tfs Build 2010

In a previous post I dealt with the creation of a Custom Activity to use in TFS2010 builds, in that example I did not dealt about logging. Logging is a vital task to do in custom action, because it is quite difficult to attach a debugger to the Build Agent, and if a build fails, it is really important to be able to understand what is gone wrong.

If you want to log from a custom action you can use this simple function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 private void LogMessage(String message, CodeActivityContext context)
        {
           BuildInformationRecord<BuildMessage> record =
             new BuildInformationRecord<BuildMessage>()
             {
                 Value = new BuildMessage()
                    {
                        Importance = BuildMessageImportance.High,
                        Message = message,
                    },
             };

            context.Track(record);
        }

Thanks to this function I can use with my custom activity, here is how I use it in my XmlPoke activity, used to change content of an xml file.

1
LogMessage("XPoke on the file " + filePath, context);

Thanks to this message I can find information on the build log.

image

This is really useful because I can, in this example, verify witch file was changed by my action simply looking at the build log.

alk.

Tags: TfsBuild ContinuosIntegration