Perform tracking and debugging on . NET

Asked

Viewed 81 times

2

During the development of an application, there are times when I get lost and want to understand how my code is running. For example, in which iteration of a particular loop of repetition it is, what is the value of a given variable.

Until then I did this by displaying dialog windows, which I then have to remove from the code. There are better ways to do this?

I’ve heard of namespace System.Diagnostics, about trace / debug, tracking and debugging and also on the method Debug.WriteLine(), but about the latter, I could not learn to use, because I could not find where he writes the information. What is the most efficient way to solve this problem?

  • 2

    If you put one breakpoint in your code (Click to the left of the line number or F9 in Visual Studio, put in the line that the courses are), and start the application in debug mode (F5 in Visual Studio), your code will stop when it reaches the line of breakpoint. You can see the values of all variables that currently exist, in addition to the properties of the instantiated classes, etc.

1 answer

3


In many cases use the mechanism of debug from Visual Studio is enough. It is extremely powerful and if people learn to use it they will be much more productive, they will make better codes and learn things just because of this.

In fact the classes Trace and Debug can be very useful to provide additional information and facilitate understanding of the code and even the use of Debugger.

The first class will always be included in the code, so using almost always should be temporary, or should be used to logging and on something absolutely necessary.

The second is not included in release mode, And that’s pretty cool, because you don’t have to keep putting it in and out of the code when you need it. Of course this is useful in more permanent test situations, which may be more interesting other techniques like contracts, unit tests and the like.

In accordance with the documentation of Debug.WriteLine it is possible set up where it will be written. The default is to send to the Debug.Log() which can be shown in the Debug Visual Studio or other tool, and standard output for debug windows that will keep in the OS event system (Event Viewer).

Visual Studio Output

  • I was left with a question, if I put the Debug class inside an if, for example, "if the variable X is equal to 12, run Debug.Writeline". In the release it will remove only the debug line and leave the if?

  • 1

    Probably only the debug, but if you’re left with nothing inside you might have some optimization that eliminates the if or manages at least one Warning. Anyway this feature should not be used this way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.