This post describes a quick solution to launching a new Visual Studio instance for debugging the code. Where I’ve found this exceptionally useful, has been in debugging code-first migration’s (one of the ways for database initialiation in .NET) Seed-method. It is by default undebuggable, as when you are running Update-Database you can’t really use a -debug switch or anything, and there’s really no way to launch the debugger. Hence the best you can do is using -verbose to get more information.
However, if you actually want to see what is happening in the code, here’s the solution.
Solution: launcing the debugger programmatically
Okay – this is quick and dirty, but still pretty darn cool. You can make your C# code launch a new Visual Studio instance to debug what you’re doing, by inserting the following line anywhere in your code:
That alone is kind of dirty, and actually might end up causing recursive debugger launches – so you probably don’t want that! You’ll need a bit more code around the call, to make sure you only start debugging when it actually make sense.
In my case I had a variable _debug which, in some edge cases, was true, and if I didn’t have a debugger already attached, I wanted to attach one. Hence the following code:
if (_debug && System.Diagnostics.Debugger.IsAttached == false) System.Diagnostics.Debugger.Launch();
This code will nicely fire open a new instance of Visual Studio and start debugging whatever’s at the function where you invoked the call. It’s kind of slow – it’ll actually open up a new instance of Visual Studio, and as we all know, that tends to take a while! After Visual Studio opens up, it should make it possible for you to debug row by row, starting from where you launched it. You can also set breakpoints as you wish, and that should work as usual!
Got this awesome tip from Stack Overflow.
Latest posts by Antti K. Koskela (see all)
- How to fix an Office Group with no Owners - November 14, 2018
- Help! My “Content and Structure” page in SharePoint Online is gone! - November 7, 2018
- Thanks for coming to my session at SPS New England 10/20 ! - October 20, 2018
- Speaking at SPS New England on 10/20! - October 17, 2018