开发者

In VS, make print-on-breakpoint use the console

开发者 https://www.devze.com 2023-04-13 07:26 出处:网络
I\'ve made a \"when hit, print a message\" breakpoint in VS 2010. It works, but it only outputs to the VS \"output\" pane. Can I make it use my app\'s console window?

I've made a "when hit, print a message" breakpoint in VS 2010. It works, but it only outputs to the VS "output" pane. Can I make it use my app's console window?

I've tried:

Debug.Listeners.Add(new ConsoleTraceListener());

As well as:

var writer =开发者_Python百科 new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(writer);


It is possible to print this message in your app console window, but for that you need to use the debugger evaluator:

  1. Create a method that you would like to call from the debugger when the breakpoint is hit.
  2. Place a breakpoint, but instead of providing just a text message use your method name in curly braces, eg. {CallFromDebugger()}

Have a look at this code:

    static void Main(string[] args)
    {
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine(i);
        }
        Console.ReadKey();
    }

    [Conditional("DEBUG")]
    static void MessageFromDebugger(string message)
    {
        Console.WriteLine("I was called from the debugger evaluator: {0}", message);
    }

If you place a breakpoint on line 5: Console.WriteLine(i); with When Hit... property set to: {MessageFromDebugger("message from address $ADDRESS")} you should see in your console window:

0
I was called from the debugger evaluator: message from address ConsoleApplication1.Program.Main(string[]) + 0x00000048
1
I was called from the debugger evaluator: message from address ConsoleApplication1.Program.Main(string[]) + 0x00000048
2

What's interesting is that you may pass arguments to your function that are valid in the calling scope as well as the special debugger variables (such as $ADDRESS, $PID, $CALLSTACK etc.). I observed though that special debugger variables are just placeholders and are replaced before submitting to your function, so remember to put them in double quotes, eg. {MessageFromDebugger(@"$CALLSTACK")}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号