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"?>
<configuration>
<configSections>
<section name="DataCollectorConfiguration"
type="Microsoft.VisualStudio.TestTools.Execution.DataCollectorConfigurationSection,
Microsoft.VisualStudio.QualityTools.ExecutionCommon,
Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<DataCollectorConfiguration xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<DataCollector typeUri="datacollector://ShaiRaiten/LocalLogsCollector/1.0">
<DefaultConfiguration>
<Folder Path="C:\Logs\"/>
<Folder Path="C:\Logs2\"/>
</DefaultConfiguration>
</DataCollector>
</DataCollectorConfiguration>
</configuration>
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)
result.Add(file);
}
}
}
return result;
}
Download Demo Project