John
John

Reputation: 285

Debugging: get path of execution

I was wondering if there is any way to return the path that Visual Studio takes when running through a program. For example, without setting breakpoints, is it possible to know exactly what happens when I click a button on a web app I created? Like if I clicked a button that had an action, is there some kind of info that Visual Studio can spit out that tells me what happened? (which functions in which files were used, etc. Essentially the "code path").

To add on to my question, I'd like to use something like this to help debug an issue. I want to know exactly what is happening when I perform some kind of action (button click in my example). If I know every "code path" I can better address the issue I'm having.

Upvotes: 2

Views: 862

Answers (3)

PiotrWolkowski
PiotrWolkowski

Reputation: 8782

You can print out a call stack results to the output window in Visual Studio. You will still need to use breakpoints but the ones that doesn't stop the application. To do that:

  1. Find the place in your code when you would like to get the stack printed out. For instance at the end of button clicked handler.
  2. Place a break point there.
  3. Right click the breakpoint. From the context menu select "When Hit..." option.
  4. In the window that will show up select "Print a message:" checkbox. It will enable a textbox. In the textbox type in: $CALLSTACK.
  5. Check the "Continue execution" checkbox if you don't want to stop the execution while printing the results.

I don't remember if the "When Hit..." option is available in the Express edition of Visual Studio.

There are also some other data you can print out without breaking execution, e.g.: previous function, process id, etc. The details are actually available in the "When Hit..." window.

Upvotes: 0

adv12
adv12

Reputation: 8551

Converting my comment to an answer so there is an answer that addresses the original question:

Use a performance profiler, which is designed to observe execution paths (typically to help you find bottlenecks, etc., but it will work fine for your purposes too). A good performance profiler will let you dig down and see, for any method, what methods it calls, how frequently, etc.

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

If I understand correctly your question, you're looking for Environment.StackTrace

The StackTrace property lists method calls in reverse chronological order, that is, the most recent method call is described first, and one line of stack trace information is listed for each method call on the stack. However, the StackTrace property might not report as many method calls as expected due to code transformations that occur during optimization.

Something else you can do is adding debug log entries to know what's is going on. It may be more useful since you could add extra custom data.

Not sure if you already have a log framework on your project. If you don't have any log4net could be a good option

Upvotes: 1

Related Questions