9

All right, I've inherited some code and the guy who started writing the code has a bad habit of squelching exceptions, thus making my life difficult. So as I look through the code I'm trying to toss something in the catch blocks so I can figure out what's going on. Since this is a Windows Forms application, I cannot write to the console itself, however I can write to the Debug console. I'd like to change the text foreground color when I hit one of these (previously) squelched exceptions so that I can easily pick them out as I debug. Of course, the other reason for writing to the debug console is so that the customer does not see these messages in the final design. Is there any way to do this? I've tried the following code, but it does not quite do it.

catch 
     {
        ConsoleColor tempColor = Console.ForegroundColor;
        StackTrace stackTrace = new StackTrace();
        Console.ForegroundColor = ConsoleColor.Red;
        System.Diagnostics.Debug.WriteLine("Exception Thrown: " + stackTrace.GetFrame(0).GetMethod().ToString());
        Console.ForegroundColor = tempColor;
     }
3
  • 1
    Easier than using the debug console: Press Ctrl + Alt + E, choose "Thrown" under "Common Language Runtime Exceptions". So even if you squelch the errors, you (the developer) will see them. Commented Jun 10, 2014 at 17:48
  • True, but there are a couple problems with this. I've been noticing quite a bit of unhandled exceptions, so it yells at me with a pop-up every time an exception gets thrown, which can be annoying to deal with (It's kind of like when you mass copy files and Windows yells at you because there's a duplicate of files a.txt, b.txt ... z.txt one at a time. At some point it becomes a "boy who cried wolf" type deal). The other, and more important issue is that the software I'm working on makes heavy use of DLL's, so the debugger can't tell me where the exception was thrown, only the DLL name. Commented Jun 10, 2014 at 17:54
  • Maybe I'm wrong but I velive you don't have any control over debug console. A workaround instead of changing colors is to append some header (something like [^^^^^^ERROR^^^^^]) to catch them visually Commented Jun 10, 2014 at 17:57

2 Answers 2

8

There doesn't seem to be any way to change the color.

However using the category option of the WriteLine method will allow you to format a header line and a message line:

Debug.WriteLine("\n\tError Message","Error Message 1");

The output should be something like this:

Error Message 1:
    Error Message
Sign up to request clarification or add additional context in comments.

Comments

8

Its may be too late but there is a Tool (which I have not tried :( ) https://marketplace.visualstudio.com/items?itemName=MikeWard-AnnArbor.VSColorOutput

I use http:// prefix to get my debug.writeline messages colored.

Debug.WriteLine("http://MyMessage")

1 Comment

This is dirty ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.