Custom TraceSource and debugging using IntelliTrace

来源:转载

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.

Code:

using System;using System.Diagnostics;namespace CustomTracing{ class TraceListenerForIntelliTrace : TraceListener { ///<summary> /// A class scope variable that holds the contents of the current /// trace message being sent to this custom TraceListener. /// We can expect multiple .Write(string) calls and one final /// .WriteLine(string) call that will signify the end of the message. ///</summary> private string message = String.Empty; public override void WriteLine(string message) { // Since we are told to WriteLine now, it means // this is the last part of the message this.message += message; this.WriteMessage(this.message); // Since we just wrote the last part of the messsage // reset the class scope variable to an empty string // to prepare for the next message this.message = String.Empty; } public override void Write(string message) { // Since we are told to just Write and not WriteLine // it means there is more to come for this message, so // use a class scope variable to build up the message this.message += message; } public void WriteMessage(string message) { // Do nothing here, we just need the method to exist // so that IntelliTrace can hook into it and extract // the parameter "message" } }}

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><source name="TraceTest" switchName="SourceSwitch"switchType="System.Diagnostics.SourceSwitch" ><listeners><add name="TraceListenerForIntelliTrace" type="CustomTracing.TraceListenerForIntelliTrace, CustomTracing"/><remove name ="Default" /></listeners></source></sources><switches><!-- You can set the level at which tracing is to occur --><add name="SourceSwitch" value="Information" /><!-- You can turn tracing off --><!--add name="SourceSwitch" value="Off" --></switches></system.diagnostics>

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]/Common7/IDE/CommonExtensions/Microsoft/IntelliTrace/[Version of your VS]/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 areusing MMAthen 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:

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