You ever need to a piece of code, BUT just for debugging?
Apr
30
Written by:
Thursday, April 30, 2009 7:45 PM
Have you ever wanted to write a piece of code JUUUUUUUST for debugging purposes? Ever been burnt with leaving it in for a Release build? Read on for a really quick'n' easy way to avoid this in the future!
Come on now! Admit it! We've all done it!
We've all put in a printf() statement, or Console.WriteLine() or something to that affect to "just peek at a variable quickly! I'll pull it out right after I'm done!" Next thing you know, you're going down that rabbit hole so far, you've lost track of where you can from, 10-15 Console.WriteLine's later, you've found 14 of them! DOH! You missed one! How did you find that out? Cause a customer is calling (or worse, your boss) about why they're seeing a certain piece of cryptic text.
A bit embarassing? Strike a little too close to home? Check out this quick'n'dirty little precompiler command to help save yoursef from that in the future! Use the #if DEBUG precompile statement to enter stuff JUUUUST for the DEBUG build. It's not compiled in for RELEASE builds.
#if DEBUG //sets up VS for debugging, when you see the dlg popup then you can pick your VS or a new one to debug with
System.Diagnostics.Debugger.Break();
#endif
You could use the Debugger.Launch() if you're developing something like a Windows Service where you're running the application outside of Visual Studio but want it to hook up with a debugging instance of VS. But if you're doing regular WinForms/WPF apps, the Debugger.Break() should work just great for you.
I hope this little tidbit will help save you a bit of embarassment in the future. Now go grab a coffee and get coding! :>