简体   繁体   English

使用代码在 Visual Studio 调试控制台中更改颜色

[英]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.所以当我查看代码时,我试图在catch块中抛出一些东西,这样我就可以弄清楚发生了什么。 Since this is a Windows Forms application, I cannot write to the console itself, however I can write to the Debug console.由于这是一个 Windows 窗体应用程序,我无法写入控制台本身,但是我可以写入调试控制台。 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:但是,使用WriteLine方法的类别选项将允许您格式化标题行和消息行:

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它可能为时已晚,但有一个工具(我还没有尝试过:() https://marketplace.visualstudio.com/items?itemName=MikeWard-AnnArbor.VSColorOutput

I use http:// prefix to get my debug.writeline messages colored.我使用http://前缀来为我的 debug.writeline 消息着色。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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