简体   繁体   中英

Changing color in Visual Studio Debug Console With Code

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;
     }

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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM