Custom TraceSource and debugging using IntelliTrace

Angelos Petropoulos

Angelos

IntelliTrace in Visual Studio Enterprise comes with support for tracing out of the box. All you have to do is make sure the appropriate IntelliTrace events are enabled in VS settings and IntelliTrace will capture trace statements as events.

When you are not using the default Debug.Trace methods and are instead using a custom TraceSource, you don’t want to rewrite your tracing code just to get the trace data captured by IntelliTrace. This blog post will explain how to add support for events from a custom TraceSource in addition to the events listed in VS settings (see screenshot above).

If you’d like, you can download the source code and follow along as well as the .itrace file that was generated.

Overview

  1. Create a custom TraceListener specifically for IntelliTrace
  2. Enable the custom TraceListener for your application
  3. Update the collection plan XML file to tell IntelliTrace about the custom TraceListener
  4. IntelliTrace will now pick up all messages that are sent to the custom TraceListener

Step 1 – Create a custom trace listener specifically for IntelliTrace

Create a class that extends TraceListener (you will need a reference to System.Diagnostics). In this example we are using the namepace “CustomTracing”, we will need to know this later on. You can use a difference namespace for your TraceListener, but make sure you use that same namespace whenever we are using “CustomTracing” throughout this example.

Code:

Step 2 – Enable the custom TraceListener for your application

Make the following changes to app’s config. The highlighted parts will need to be updated with the appropriate namespace and class name that you choose for your custom TraceListener.

Step 3 – Update the collection plan XML file to tell IntelliTrace about the custom TraceListener

Add the following XML fragment just before the </ModuleSpecifications> closing tag. The highlighted part will need to be updated with the name of the assembly that contains your custom TraceListener.

Add the following XML fragment just before the </DiagnosticEventSpecifications> closing tag. The highlighted parts will need to be updated with the appropriate namespace and class name that you choose for your custom TraceListener.

Where to make this change when using F5/Attach

If you are using F5/Attach to debug the application, then you need to change the “collectionplan.xml” file found here:
C:Program Files (x86)Microsoft Visual Studio [Version of your VS]Common7IDECommonExtensionsMicrosoftIntelliTrace[Version of your VS]en

For example, for VS 2013:
C:Program Files (x86)Microsoft Visual Studio 12.0Common7IDECommonExtensionsMicrosoftIntelliTrace12.0.0en

Where to make this change when using the IntelliTraceStandalone Collector

If you are using the IntelliTrace Standalone Collector then you need to change the collection plan .xml file that you pass in as an argument using PowerShell. More information on how to use the standalone collector.

Where to make this change when using Microsoft Monitoring Agent (MMA)

If you are using MMA then you need to change the collection plan .xml file that you pass in as an argument using PowerShell and the Start-WebApplicationMonitoring command. If you are not used to providing a custom collection plan, you can find the default collection plans here:
C:Program FilesMicrosoft Monitoring AgentAgentIntelliTraceCollector

Where to make this change when using Azure

When you publish your project to Azure, you get the choice to enable IntelliTrace and change its settings

Clicking “Settings…” shows this dialog which looks just like the Visual Studio settings for IntelliTrace, but it’s not quite the same:

As a result of enabling IntelliTrace and changing the settings a new collectionplan.xml file will appear here:
%appdata%MicrosoftVisualStudio12.0Cloud Tools

This is the actual XML file that gets uploaded to Azure. So it’s this XML file that you need to add the XML fragment to for the custom TraceListener.

Step 4 – IntelliTrace will now pick up all messages that are sent to the custom TraceListener

My demo console app below is trying to write 3 different trace messages

Code:

so I expect IntellITrace to pick up 3 separate trace events

Bonus Step 5 – Customizing your trace messages

You have two ways to customize the way your trace messages are captured by IntelliTrace:

  1. Manipulate the string within your custom TraceListener class before calling method WriteMessage(string message) is called.
  2. Change the .xml collection plan file, and more specifically the elements ShortDescription (what IntelliTrace shows in VS while the event is collapsed) and LongDescription (what IntelliTrace shows in VS while the event is selected and expanded
This just prints out the trace message as a whole:

This changes the long version of the trace message:

We are always looking for feedback and comments for our features. Please send us a tweet or visit the MSDN Diagnostics forums.

CustomTracing (source code).zip

Angelos Petropoulos
Angelos Petropoulos

Product Manager, on the .NET team

Follow Angelos   

No Comments.