I had an idea of something I wanted to post, but a quick google turns up I posted it 6 months ago, so here are some debugging tips.

Console Break

So you are writing a console app? Cool. Want to see the output? Of course, you do. But when you run it, the app pops up then disappears really quickly right? No? Have you a break point in the last line? Well, that’s sensible.

But what if you do not want to remember to put that break point in everytime git cleans your directory, or someone pulls the code for the first time?

How about this?

class Program
{
    static void Main(string[] args)
    {
        //App goes here

        Console.ReadLine();
    }
}

Nice trick! Now every time the app runs, it waits for a keypress to continue. But what about when you run the app for real. Damn! I didn’t really want the app to wait for a key to exit… I’ve got it!

class Program
{
    static void Main(string[] args)
    {
        //App goes here


        if(Debugger.IsAttached)
        {    
            Console.ReadLine();
        }
    }
}

Now, it will only stop if the debugger is attached. Perfect.

Performance overhead you say? Really? I wouldn’t be worried but if you want we can do this:

class Program
{
    static void Main(string[] args)
    {
        //App goes here


        BreakOnDebug();
    }

    [Conditional("DEBUG")]
    private void BreakOnDebug()
    {
        if (Debugger.IsAttached)
        {
            Console.ReadLine();
        }
    }
}

Happy?

You can also try using if (Debugger.IsAttached) {Debugger.Break(); } instead of ReadLine to force the code to actually stop and to start debugging the state of your application instead. Although to be fair, at the end of the app, there probably isn’t much to see.

Command-line attach

But I really want to be able to run this at the command line, from a specific folder, and play with the arguments. And I just found it crashes for some reason. Well, I let it attach to VS automatically (the dialog does come up), but what I really want to do, is run it attached to the debugger and step through. How about this?

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Attach debugger and press any key to continue");
        Console.ReadLine(); 

        //breakpoint goes here

        //App goes here
        ...    
    }
}

Now I can run my app and it stops before starting. I can use the debug => attach to process… menu it Visual Studio to attach, like I would to an IIS site, but instead to the running instance of my console app. Easy. Press a key in the console and any of my breakpoints and I can step through the specific parameters running from the specific folder and get the bottom of this bug.

The only catch here is to not forget to remove the code when not running debug builds. Or we could use the same trick again:

class Program
{
    static void Main(string[] args)
    {
        StopAndWaitOnDebug();

        //breakpoint goes here

        //App goes here
        ...    
    }
    
    [Conditional("DEBUG")]
    private void StopAndWaitOnDebug()
    {
        Console.WriteLine("Attach debugger and press any key to continue");
        Console.ReadLine();
    }
}

You can also try using if (Debugger.IsAttached) {Debugger.Break(); } as well after the ReadLine if you really wanted to.

So there you go. Some nice and useful ways to use the Console.ReadLine method for debugging.