How do I write color text to the Visual Studio output window from c#?

前端 未结 7 1969
忘了有多久
忘了有多久 2020-12-19 06:16

I want to write color text to the Visual Studio output window from c#. I want to output red code from my unit tests.

相关标签:
7条回答
  • 2020-12-19 06:58

    I found this question while trying to figure out why some of the lines of text in my Visual Studio 2017 Output window are colored red, and how I could accomplish the same thing.

    I found that I was able to get red text by writing out a line which included:

    • an instance of "Error:" (Error, colon, followed by a space)
    • (other characters can go here)
    • another instance of "Error:" (Error, colon, followed by a space)
    • (other characters can go here)
    • followed by 1 instance of "Error *" (Error, followed by a space and then some other character)
    • (other characters can go here)

    An example:

    Debug.WriteLine("Error: This line will be red Error: Error Jeff");
    
    0 讨论(0)
  • 2020-12-19 07:02

    Edited The code below works to display output in Windows Console, not to Visual Studio Output window (thanks @AnthonyLambert for correcting me).

     Console.ForegroundColor = ConsoleColor.Magenta; //Choose from the Enum
     Console.WriteLine("This message is to be in Magenta!");
     Console.ResetColor();//Reset to default
    
    0 讨论(0)
  • 2020-12-19 07:06

    Actually there are extensions for that. I use the lite (free) version of VSCommands for Visual Studio 2010. With the Pro version a regex could be set to make the colouring. In the lite version I add "warning" text to the debug message and it is written in light brown.

    0 讨论(0)
  • 2020-12-19 07:07

    Far as I know, you can't do that. You can change the Console's font colour, but it has no effect on Visual Studio's output window.

    The only way to change the text colour there is through Tools->Options->Fonts and Colors, and that affects all the text that is sent to the window.

    0 讨论(0)
  • 2020-12-19 07:12
    SetConsoleTextAttribute(hConsole, x)
    

    Where k is an integer color value and hConsole is a standard output handle.

    More Here - easier commands

    0 讨论(0)
  • 2020-12-19 07:15

    As far as I know, the output window in Visual Studio is a "simple textbox" type control that doesn't support colored text.

    0 讨论(0)
提交回复
热议问题