How To Create Custom DataCollector In Visual Studio 2010 – Part 2


How To Create Custom DataCollector In Visual Studio 2010 – Part 2

In the previous post (How To Create Custom DataCollector In Visual Studio 2010 – Part 1)I showed how to create a Custom Data Collector using Visual Studio 2010, Here we continue improving our Data Collector using external Xml Configuration files.

Instead of add hardcoded values into your code use the Xml to take and save user settings.

Download Demo Project

Step 1 – Add Configuration File

To collect a log file when a test finishes based on what the user configured in test settings, you must create an App.config file and add it to your solution

Copy below format into the app config file:

<?xml version="1.0" encoding="utf-8"?>



    <section name="DataCollectorConfiguration"



      Version=, Culture=neutral,

      PublicKeyToken=b03f5f7f11d50a3a" />


  <DataCollectorConfiguration xmlns="">

    <DataCollector typeUri="datacollector://ShaiRaiten/LocalLogsCollector/1.0">


        <Folder Path="C:\Logs\"/>

        <Folder Path="C:\Logs2\"/>






Make sure the DataCollector typeUri attribute is the same as [DataCollectorTypeUri("datacollector://ShaiRaiten/LocalLogsCollector/1.0")] from the Data Adapter class.

Step 2 – Initialize Configuration Element

In the Initialize method make sure to Initialize the Configuration XmlElement .

private XmlElement configurationSettings;

public override void Initialize(XmlElement configurationElement, DataCollectionEvents events, DataCollectionSink sink,

DataCollectionLogger logger, DataCollectionEnvironmentContext environmentContext)


    configurationSettings = configurationElement;

    events.SessionEnd += new EventHandler<SessionEndEventArgs>(events_SessionEnd);


Step 3 – Use Configuration Element


void events_SessionEnd(object sender, SessionEndEventArgs e)


    // Get any files to be collected that are

    // configured in your test settings

    List<string> files = getFilesToCollect(e.Context);


    // For each of the files, send the file to the data sink

    // which will attach it to the test results or to a bug

    foreach (string file in files)


        dataSink.SendFileAsync(e.Context, file, false);





// A private method that returns the file names

private List<string> getFilesToCollect(DataCollectionContext context)


    XmlNodeList folders = configurationSettings.GetElementsByTagName("Folder");


    // Build the list of folder to collect from the

    // "Path" attributes of the "Folder" nodes.

    List<string> result = new List<string>();

    foreach (XmlNode folder in folders)


        XmlAttribute pathAttribute = folder.Attributes["Path"];

        if (pathAttribute != null && !String.IsNullOrEmpty(pathAttribute.Value))


            if (Directory.Exists(pathAttribute.Value))


                string[] files = Directory.GetFiles(pathAttribute.Value);

                foreach (string file in files)





    return result;


Download Demo Project

Add comment
facebook linkedin twitter email

Leave a Reply

one comment

  1. Michael Bendtsen28/02/2012 ב 21:17

    Is it possible to create custom data collectors that are used in an environment? Or is this only support running the tests locally.

    Best regards,