简体   繁体   中英

Can a non-console app also produce colored output in the Visual Studio output window?

I have a webapi project that I would like to output colored output in the Visual Studio 'output window' while I'm debugging.

In a console application you can do this as follows

Console.BackgroundColor = ConsoleColor.Green;
Console.Write($"test");

But in a webapplication where I use Debug.WriteLine();this does not work. Is there some way in which the TextWriterTraceListener that outputs to the console can receive instruction to output color?

I have found a VS Plugin that might provide a 'hack' on Line basis, but that's not what I want.

If you use the ILogger , you'll get log messages in your console.

Per your example:

Console.BackgroundColor = ConsoleColor.Green;
_logger?.LogInformation($"test");

See also: https://learn.microsoft.com/en-us/as.net/core/fundamentals/logging/?view=as.netcore-7.0

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