Using Trace points for performing custom action during debugging in Visual Studio

Using Trace points for performing custom action during debugging in Visual Studio

Trace Point is nothing but a breakpoint with some actions. When Action is added into a breakpoint, Visual Studio would execute them during debugging. You do also have and option to continue the execution without pausing it or pause it. This is quite helpful when you want to inspect values, object without interrupting the debugging. Incase of breakpoint, the execution does pause and then you need to inspect them by your own.  Where as for Trace Point, you can have a pre-defined actions setup to be execute during the debugging session.

Let’s try to understand this with one simple example.  Consider a class with Main(), method having three additional method A(int), B() and C().   Now to check when A() is called and what is the value has been passed to it, we use a breakpoint and then use DataTip or Watch Windows to inspect the values.  In other cases, we use Debug.WriteLine to write values in the output windows as shown in below screenshot.

image

Now to achieve this in an other way and without impacting your debugging flow, you add the actions to those breakpoints.

First, Right Click on the “Breakpoint” and select  “Actions

image

 

This will open the Breakpoint Settings window where you can provide the custom actions. In this case I have mentioned to log the “Function name and Parameter passed into it” . Then you have the option “Continue Execution” to set true or not.

 

image

 

With these, the breakpoint icon will change into a diamond which indicate there are some action attached with it.  Now if you run the app, debugger won’t stop, but will log the requested message in output windows.

image

 

Similarly, you can attach multiple Trace point to log your information.

image

 

The Actions text window has intellisense enabled. So incase of custom object you should be able to write it easily.

image

 

Not only the function name, it has some predefined attributes like Address, Calls Stacks, PID defined, which you can use as and when you need.

image

This Trace point could be tremendous helpful when you are working with multiple threads.

Incase you are interested to look bit inside, you can export these Trace Point using the “Export” option available in the context menu. Open the saved xml, you will notice the information are saved as part of BreakPointCollection, and BreakPoint entity. The only difference you would notice the “IsTracePointAction” is set to 1.  Incase of normal breakpoint it will be 0.

image

Hope this helps !

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

3 Comments to “Using Trace points for performing custom action during debugging in Visual Studio”

Comments are closed.