DataCollection support is added in test platform 15.3.0 onwards. It is part of
VS 2017 15.3 and dotnet-cli 2.0.0 builds.

DataCollector

A DataCollector is a test platform extension to monitor test run. It can be extended to perform tasks on specific test execution events. Currently, four events are exposed to DataCollector:

Session Start event.

Test Case Start event

Test Case End event.

Session End event.

You can author a DataCollector to collect code coverage data for a test run, to collect logs when a test case or test run fails, etc. These additional files are called Attachments and they can be attached to test result report(trx).

Please refer here for instructions on creating a DataCollector and here
if you're interested in the architecture of data collection.

Configure DataCollectors

DataCollectors can be configured for monitoring test execution through runSettings, testSettings or vstest.console args.

Using vstest.console args

DataCollectors can be configured and used through first class command line arguments /collect and /testadapterpath. Hence, for common DataCollection scenarios, separate runsettings file may not be required.

Below is the sample command to configure and use DataCollectors through vstest.console command line.

Please note that testadapterpath is not required for DataCollectors shipped along with TPv2.

Using TestSettings

While the recommended way is to use runsettings or vstest.console args, there are few DataCollectors which only worked with testsettings.
E.g.: System Information DataCollector. Below is the sample testsettings for using System Information DataCollector.

Key differences for using DataCollectors in TPv2 v/s TPv1.

In TPv1, DataCollectors are loaded from <<VisualStudio Installation Directory>>\Common7\IDE\PrivateAssemblies\DataCollectors.
In TPv2, DataCollectors are loaded from TestAdaptersPaths specified in runSettings or /testadapterpath argument of vstest.console.exe. DataCollector assemblies must follow the naming convention *collector.dll.

Previous DataCollector settings will continue to work, but additional TestAdaptersPaths must be specified in runsettings if DataCollector is not shipped along with TPv2. TestAdapterPath can also be specified through vstest.console args from command line.

There are breaking changes in latest DataCollector interface. Hence, older DataCollectors need to be rebuilt against latest APIs to work with TPv2. For details, refer here(todo);

Working with Code Coverage

Requirements:
Code Coverage requires the machine to have Visual Studio 2017 Enterprise (15.3.0 or later installed and a Windows operating system.

Setup a project

Here's a sample project file, please note the xml entity marked as Required. Previously, the Microsoft.VisualStudio.CodeCoverage was required, but is now shipped with the SDK.

This will generate a *.coverage file in the <Current working directory>\TestResults directory.

Event Log Data Collector

This document introduces Event Log DataCollector. We will start with a brief overview of Event Log DataCollector, use cases where it will be useful followed by steps to enable it.

Introduction

Event Log DataCollector is a Windows only DataCollector that is used to get event logs logged into Windows Event Viewer during test execution. Event logs are saved in a file Event Log.xml and this file is available as Attachment as part of test result report (trx).
When enabled, Event Log DataCollector generates one Event Log.xml file for entire test session. Event Log.xml files are also generated corresponding to all test cases as well, to provide a granular view of events logged while executing a test case.

The above runsettings will collect event logs from System and Application event logs which are logged as Error or Warning and event source is specified as CustomEventSource. MaxEventLogEntriesToCollect specifies the upper limit on the events that are logged in Event Log.xml file corresponding to test cases. There is no upper limit on number of events logged in Event Log.xml file for test session.

In default configuration (through vstest.console.exe args or when section is empty in runsettings), System, Application and Security logs with entry types Error, Warning or FailureAudit and with any event source are collected. Default value of MaxEventLogEntriesToCollect is 50000. There is no upper limit on number of events logged in Event Log.xml file for test session.

A note on Security Event Log

Please note that Security event logs can only be collected if the account under with tests are run has admin privileges.