Debugging in C# – Adding Debug Code for Better Error Information

cdebugginggitnet

When our application doesn't work the way we expect it to (e.g. throws exceptions etc.), I usually insert a lot of debug code at certain points in the application in order to get a better overview of what exactly is going on, what the values for certain objects are, to better trace where this error is triggered from. Then I send a new installer to the user(s) that are having the problem and if the problem is triggered again I look at the logs and see what they say.

But I don't want all this debug code to be in the production code, since this would create some really big debug files with information that is not always relevant.

The other problem is that our code base changes, and the next time, the same debug code might have to go in different parts of the application.

Questions

Is there a way to merge this debug code within the production code only when needed and have it appear at the correct points within the application?

Can it be done with a version control system like git so that all would be needed is a git merge?

P.S. The application I'm talking about now is .NET, written in C#.

Best Answer

For the VCS side:

  1. In case of Git you have to have two separate branches for clean and debug versions. Ordinary work happens in master, debugging - in debug branch (or bugfix branch, branched from debug) with added debug information.

  2. During lifecycle you permanently merge changes from master to debug, thus - have two versions, which differ only in debug information amount. After bug-hunting you backmerge (from debug to master) only relevant part of changes from branch.

BTW: Mercurial with MQ require less frictions around branching-merging and use only one mainline branch, while allow to use insertions of debug code