VSTS 2010 Beta 2 – Diagnostic Data Adapters – Custom Adapter – Part 2

17/01/2010

VSTS 2010 Beta 2 – Diagnostic Data Adapters – Custom Adapter – Part 2

In my previous post Diagnostic Data Adapters – Create Custom Adapter – Part 1 I’ve showed how to create a Custom Data Adapter, this post will be on how to add a Configuration File to your Custom Data Adapter.

Download Demo Project

Step 1 – Add Configuration File

In your Data Adapter project add a new xml file and change the name to <name of your data diagnostic adapter>.dll.config

Copy below format into the xml config file:

<?xml version="1.0" encoding="utf-8"?>
<Config
>
  <DataCollector typeUri="datacollector://ShaiRaiten/LogsCollector/1.0" enabledOnCollectionOnlyAgents="true"
>
    <DefaultConfiguration
>
      <Folder FullPath="C:\Logs\Day"
/>
      <Folder FullPath="C:\Logs\Night"
/>
    </DefaultConfiguration
>
  </DataCollector
>
</Config>

 

Make sure the DataCollector typeUri attribute is the same as [DataCollectorTypeUri("datacollector://ShaiRaiten/LogsCollector/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 config;

 

public void Initialize(System.Xml.XmlElement configurationElement, DataCollectionEvents events, DataCollectionSink dataSink, DataCollectionLogger logger, AgentContext agentContext)

{

    this.config = configurationElement;

 

    Events.TestCaseEnd += new EventHandler<TestCaseEndEventArgs>(Events_TestCaseEnd);

}

Step 3 – Use Configuration Element

void Events_TestCaseEnd(object sender, TestCaseEndEventArgs e)

{

    try

    {

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

        XmlNode currentNode = config.FirstChild;

 

        while (currentNode != null)

        {

            if ((currentNode.Name == "Folder") && (currentNode.Attributes["FullPath"] != null))

                folders.Add(currentNode.Attributes["FullPath"].Value);

            currentNode = currentNode.NextSibling;

        }

 

        foreach (string folder in folders)

        {

            string[] files = Directory.GetFiles(folder);

            foreach(string file in files)

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

        }

    }

    catch (Exception ex)

    { throw ex; }

}

Step 4 – Install Configuration File

End you’re almost done, Build the solution, and then copy the built assembly and the Configuration file to –
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\DataCollectors

Now, Run the test and see the results below:

image

Full Code :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.VisualStudio.TestTools.Execution;

using System.Xml;

using System.IO;

using System.Diagnostics;

 

namespace LogsCollector

{

    [DataCollectorTypeUri("datacollector://ShaiRaiten/LogsCollector/1.0")]

    [DataCollectorFriendlyName("Local Logs Collector")]

 

    public class LogsCollector : IDataCollector

    {

        private DataCollectionEvents Events;

        private DataCollectionSink dataSink;

        private XmlElement config;

 

        public void Initialize(System.Xml.XmlElement configurationElement, DataCollectionEvents events, DataCollectionSink dataSink, DataCollectionLogger logger, AgentContext agentContext)

        {

            this.Events = events;

            this.dataSink = dataSink;

            this.config = configurationElement;

 

            Events.TestCaseEnd += new EventHandler<TestCaseEndEventArgs>(Events_TestCaseEnd);

        }

 

        void Events_TestCaseEnd(object sender, TestCaseEndEventArgs e)

        {

            try

            {

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

                XmlNode currentNode = config.FirstChild;

 

                while (currentNode != null)

                {

                    if ((currentNode.Name == "Folder") && (currentNode.Attributes["FullPath"] != null))

                        folders.Add(currentNode.Attributes["FullPath"].Value);

                    currentNode = currentNode.NextSibling;

                }

 

                foreach (string folder in folders)

                {

                    string[] files = Directory.GetFiles(folder);

                    foreach (string file in files)

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

                }

            }

            catch (Exception ex)

            { throw ex; }

        }

 

        public void Dispose()

        {

            //Unregister events

            Events.TestCaseEnd -= new EventHandler<TestCaseEndEventArgs>(Events_TestCaseEnd);

        }

    }

}

 

Download Demo Project

Enjoy.

Add comment
facebook linkedin twitter email

Leave a Reply

2 comments

  1. Prem28/04/2011 ב 10:38

    Thanks ShaiRaiten,

    Really useful and short article. I also gone through other articles also. those are also really useful. keep posting

  2. Prem03/05/2011 ב 10:54

    Hi ShaiRaiten,
    Everything is working fine. With some modification I could be able to add the interface of configuration also and it working in VS. But when I test from MTM configuration interface is not working as well as inside the program “config” is null in Events_TestCaseStart and Events_TestCaseEnd. Please help me.