Debugger.Launch (.Net)
Now and then I come across a feature in .Net that I have managed to completely miss over the years. Whilst trying to figure out why a unit test was failing when running it, but passing whilst debugging, I came across this nifty feature.
<a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.launch(v=vs.110).aspx">Debugger.Launch</a> will attempt to break the program execution and give you the chance to attach a debugger so you can examine what’s going on.
if (condition)
{
Debugger.Launch();
}
If the code is already running with a debugger attached, nothing will happen when the code is hit. If you want the code to break whether or not you have a debugger attached, you can use <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break(v=vs.110).aspx">Debugger.Break</a> instead.