How To Create Custom DataCollector In Visual Studio 2010 – Part 1
How To Create Custom DataCollector In Visual Studio 2010 – Part 1
What is DataCollector - Team System 2010 Beta 2 – Diagnostic Data Adapters – No More No Repro!
If you remember I’ve already wrote on how to create a custom data collector:
VSTS 2010 Beta 2 – Diagnostic Data Adapters – Custom Adapter – Part 1, VSTS 2010 Beta 2 – Diagnostic Data Adapters – Custom Adapter – Part 2
but between Beta 2 and RTM there were couple of changes with Data Collectors.
Download Demo Project
Step 1 – Getting Started
Create a Class library project and add two references:
- Microsoft.VisualStudio.QualityTools.Common
- Microsoft.VisualStudio.QualityTools.ExecutionCommon.dll
You can find those assemblies C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0
(If you are not using 64bit operation system - C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0
Step 2 – Create Data Collector Class
1. Add new class to your project with the Data Collector name, make sure the class defined as Public and add both using to the class:
using Microsoft.VisualStudio.TestTools.Execution;
using Microsoft.VisualStudio.TestTools.Common;
2. Add the Data Collector attributes
[DataCollectorTypeUri("datacollector://ShaiRaiten/LocalLogsCollector/1.0")]
[DataCollectorFriendlyName("Local Logs Collector")]
3. Add DataCollector interface and implement it. (Beta 2 – IDataCollector)
This is what you should see:
public override void Initialize(XmlElement configurationElement, DataCollectionEvents events, DataCollectionSink sink,
DataCollectionLogger logger, DataCollectionEnvironmentContext environmentContext)
{
}
Step 3 – Implement Custom Data Adapter
Using the DataCollectionEvents add the desire events for the Custom Data Adapter.
CustomNotification Raised when a custom notification occurs.
DataRequest Raised to request intermediate data.
SessionEnd Raised when a test session ends.
SessionPause Raised when a test session pauses.
SessionResume Raised when a test session resumes.
SessionStart Raised when a test session starts.
TestCaseEnd Raised when a test case ends.
TestCasePause Raised when a test case pauses.
TestCaseReset Raised when a test case resets.
TestCaseResume Raised when a test case resumes.
TestCaseStart Raised when a test case starts.
TestStepEnd Raised when a test step ends.
TestStepStart Raised when a test step starts.
And define DataCollectionSink for storing files as attachments.
For Example:
private DataCollectionEvents dataEvents;
private DataCollectionLogger dataLogger;
private DataCollectionSink dataSink;
public override void Initialize(XmlElement configurationElement, DataCollectionEvents events, DataCollectionSink sink,
DataCollectionLogger logger, DataCollectionEnvironmentContext environmentContext)
{
dataEvents = events; // The test events
dataLogger = logger; // The error and warning log
dataSink = sink; // Saves collected data
// Configuration from the test settings
events.SessionEnd += new EventHandler<SessionEndEventArgs>(events_SessionEnd);
}
Now write the code to collect the logs from the specified folder.
void events_SessionEnd(object sender, SessionEndEventArgs e)
{
string LogPath = @"C:\Logs";
try
{
if (Directory.Exists(LogPath))
{
string[] files = Directory.GetFiles(LogPath);
foreach (string f in files)
{
dataSink.SendFileAsync(e.Context, f, false);
}
}
}
catch (Exception ex)
{ throw ex; }
}
Download Demo Project