Debugging Applications in C#

DevToolsGuy / Thursday, September 4, 2014

Developing software solutions does not only involve writing code. A significant amount of time is spent on debugging - finding and fixing errors and problems. Initially a developer needs to test and debug his own code. Then when feedback arrives from dedicated testers, they need to debug the code to find any defects highlighted there.

It is fair to say that approximately 50% of the time a developer spends working on a project is spent on debugging code. As a result, it is really important to have good debugging tools to help speed up project work.

The most widely used tool for developing C# applications is Visual Studio. There are some open-source alternatives, but Visual Studio is the best tool available. Supported by the community, it comes with a wealth of help and support information for those that need it.

Visual Studio started out as a pure development tool. Over the years new versions have been released which added a host of project analysis, test, and management features. It is now used by software designers, testers, and project managers alike. At its core, however, Visual Studio is still very much a developer tool, so here are our top tips for debugging applications in C#.

  1. Step through code and add breakpoints. See what code is executed, which variable values are assigned, and which break only at certain points when a condition is true. What is the stacktrace for any exceptions?
  2. Use Intellitrace to record code execution. Stepping through code can be time consuming. Using Intellitrace, certain actions can be recorded to get a better understanding of the history of events. See here for more information about Intellitrace.
  3. Attach to a process. It is possible to attach to every process running on the system to see what’s happening e.g. If a third-party tool is used and an error occurs, some information about the exception will be shown if Visual Studio is attached to that process.
  4. Depending on the application, extra logging can be enabled:
    • If debugging WCF services, extra tracing can be enabled, see here for a walkthrough.
    • If debugging SharePoint applications, Verbose logging can be enabled in Central Administration.
    • When debugging websites hosted in IIS, logging in IIS can also be used to debug the application.
  5. When building graphical applications that rely on DirectX, Graphic Diagnostics can help debugging. See here for a full description.
  6. Reproducing an error raised by a tester can sometimes be confusing if the developer does not get enough information. Visual Studio can be used by testers to automate their testing and share the results with developers:
    • A tester transforms a paper test case into a Coded UI test in Visual Studio. When it fails, this test case can be shared easily with the developer. This makes it a lot easier to debug for the developer.
    • If ‘performance testing’ has raised defects, a load can be simulated by using load tests.
    • A big advantage of this ‘automated testing’ approach is that by doing so, test cases can be used for automated regression tests.
    • To explore some automated testing tools for yourself, check out the ones that Infragistics offers, free for 30 days.

Useful add-ons to Visual Studio

A number of add-on tools exist that make Visual Studio even more useful:

  • Resharper is the most commonly used add-on for Visual Studio. It extends Visual Studio in many ways, e.g. automated code refactoring, code templates, easier browsing through a solution, etc. When debugging applications that rely on third party components, like applications built on SharePoint, Resharper offers the possibility to step through those third party DLL’s. It will decompile the code on-the-fly and let the developer debug the code as if it was his own code.
  • Instead of Resharper, Reflector can also be used to decompile and debug third party DLL’s.
  • DevPartner Studio extends the debugging features of Visual Studio. See here for a full list of features.
  • OzCode adds some nice features while debugging code. It improves the way values in variables are shown, and also makes it easier to see what a function returns.