IntelliTrace in Visual Studio Ultimate 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
- Create a custom TraceListener specifically for IntelliTrace
- Enable the custom TraceListener for your application
- Update the collection plan XML file to tell IntelliTrace about the custom TraceListener
- 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.
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.
<system.diagnostics><sources><sourcename="TraceTest"switchName="SourceSwitch"switchType="System.Diagnostics.SourceSwitch"><listeners><addname="TraceListenerForIntelliTrace"type="CustomTracing.TraceListenerForIntelliTrace, CustomTracing"/><removename="Default"/>listeners>source>sources><switches><addname="SourceSwitch"value="Information"/>switches>system.diagnostics>
Step 3 - Update the collection plan XML file to tell IntelliTrace about the custom TraceListener
Add the following XML fragment just before the closing tag. The highlighted part will need to be updated with the name of the assembly that contains your custom TraceListener.
<ModuleSpecificationId="custom">CustomTracing.exeModuleSpecification>
Add the following XML fragment just before the closing tag. The highlighted parts will need to be updated with the appropriate namespace and class name that you choose for your custom TraceListener.
<DiagnosticEventSpecificationenabled="true"><CategoryId>tracingCategoryId><SettingsName_locID="settingsName.Custom">Custom TraceSourceSettingsName><SettingsDescription_locID="settingsDescription.Custom">Custom TraceSourceSettingsDescription><Bindings><Binding><ModuleSpecificationId>customModuleSpecificationId><TypeName>CustomTracing.TraceListenerForIntelliTraceTypeName><MethodName>WriteMessageMethodName><MethodId>CustomTracing.TraceListenerForIntelliTrace.WriteMessage(System.String):System.VoidMethodId><ShortDescription_locID="shortDescription.Custom">"{0}"ShortDescription><LongDescription_locID="longDescription.Custom">"{0}"LongDescription><DataQueries><DataQueryindex="1"maxSize="4096"type="String"name="message"_locID="dataquery.Custom.text"_locAttrData="name"query="">DataQuery>DataQueries>Binding>Bindings>DiagnosticEventSpecification>
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]\Common7\IDE\CommonExtensions\Microsoft\IntelliTrace\[Version of your VS]\en
For example, for VS 2013:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\IntelliTrace\12.0.0\en
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 Files\Microsoft Monitoring Agent\Agent\IntelliTraceCollector
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%\Microsoft\VisualStudio\12.0\Cloud 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
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:
- Manipulate the string within your custom TraceListener class before calling method WriteMessage(string message) is called.
- 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
<ShortDescription_locID="shortDescription.Custom">"{0}"ShortDescription><LongDescription_locID="longDescription.Custom">"{0}"LongDescription>
<ShortDescription_locID="shortDescription.Custom">"{0}"ShortDescription><LongDescription_locID="longDescription.Custom">Trace captured by custom TraceListener: "{0}"LongDescription>