在VS中,使用控制台进行断点打印

发布于 2024-12-09 10:19:40 字数 297 浏览 3 评论 0原文

我在 VS 2010 中设置了一个“点击时打印消息”断点。它可以工作,但它只输出到 VS“输出”窗格。我可以让它使用我的应用程序的控制台窗口吗?

我尝试过:

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

以及:

var writer = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(writer);

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 = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(writer);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若无相欠,怎会相见 2024-12-16 10:19:40

可以在应用程序控制台窗口中打印此消息,但为此您需要使用调试器评估器:

  1. 创建一个您希望在命中断点时从调试器调用的方法。
  2. 放置一个断点,但不要仅提供文本消息,而是使用大括号中的方法名称,例如: {CallFromDebugger()}

看一下这段代码:

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

如果您在第 5 行放置一个断点:Console.WriteLine(i); 并设置了 When Hit... 属性to: {MessageFromDebugger("message from address $ADDRESS")} 您应该在控制台窗口中看到:

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

有趣的是,您可以将在调用范围内有效的参数传递给函数特别的调试器变量(例如 $ADDRESS、$PID、$CALLSTACK 等)。我观察到特殊的调试器变量只是占位符,并且在提交到函数之前被替换,所以请记住将它们放在双引号中,例如。 {MessageFromDebugger(@"$CALLSTACK")}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文