可.NET源代码硬代码的调试断点? [英] Can .NET source code hard-code a debugging breakpoint?

查看:246
本文介绍了可.NET源代码硬代码的调试断点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在.NET的方式(2.0,C尤其#)源代码触发调试休息,如果一个断点设置在这一点上,而无需记住要在那里设置一个特定的断点调试器,以及无生产运行的干扰。

I'm looking for a way in .NET (2.0, C# in particular) for source code to trigger a debugging break as if a breakpoint was set at that point, without having to remember to set a specific breakpoint there in the debugger, and without interfering with production runtime.

我们的代码需要吞下生产异常,所以我们不会破坏链接到我们的客户端应用程序,但我试图设定,让这样的错误会弹出来进行分析,如果它发生在一个调试器中运行,否则会被忽略。

Our code needs to swallow exceptions in production so we don't disrupt a client application that links to us, but I'm trying to set it up so that such errors will pop up to be analyzed if it happens to be running in a debugger, and otherwise will be safely ignored.

我的企图使用 Debug.Assert的(假)一直不太理想,我认为 Debug.Fail()会表现一样的方法。它理论上应该在生产没有任何影响,而且它成功地调试时停下来,而是通过设计有(据我可以告诉)没办法继续执行,如果你想忽略这个错误,比如你可以用一个实际的断点,并像它会在生产中做到哪里,我们吞下错误。这显然​​也打破了可变状态评价,因为调试器实际上是在原生系统的代码,而不是在我们停下来,所以它的调试帮助是有限的。 (也许我缺少让回事情看的变量和等在那里发生的一些方法。???)

My attempt to use Debug.Assert(false) has been less than ideal, and I assume that Debug.Fail() would behave the same way. It should theoretically have no effect in production, and it does successfully stop when debugging, but by design there is (as far as I can tell) no way to continue execution if you want to ignore that error, like you could with an actual breakpoint, and like it would do in production where we swallow the error. It also apparently breaks evaluation of variable state because the debugger actually stops down in native system code and not in ours, so it's debugging help is limited. (Maybe I'm missing some way of getting back into things to look at the variables and so on where it happened. ???)

我希望这样的事情 Debug.Break(),但它似乎并不存在(除非也许在.NET的更高版本),并没有其他调试方法似乎适用,无论是

I was hoping for something like Debug.Break(), but it doesn't seem to exist (unless maybe in a later version of .NET?), and no other Debug methods seem applicable, either.

更新:虽然ctacke的答案是什么,我一直在寻找最佳匹配,因为我已经还发现了一招与Debug.Assert的() - 在调试器中运行时 - 暂停调试器,去了代号为Debug.Assert的调用待定(以绿色突出显示,因为它是倒在框架代码)和命中步出式(移位-F11),然后打在断言对话框忽略。这将使暂停后,断言(并能够继续执行,如果没有发生,因为它被忽略)返回调试器。可能有其他的方式做同样的事情(不打重试更直接地做到这一点?),但这种方法是直观的。

Update: While ctacke's answer is the best match for what I was looking for, I have since also discovered a trick with Debug.Assert()--when running in the debugger--Pause the debugger, go to the code for the Debug.Assert call pending (highlighted in green because it is down in the framework code) and hit Step-Out (shift-F11), then hit Ignore in the assert dialog box. This will leave the debugger paused upon the return of the assert (and able to continue execution as if it hadn't occurred, because it was ignored). There may be other ways to do much the same thing (does hitting Retry do this more directly?), but this way was intuitive.

推荐答案

您可能有这样的事情后:

You probably are after something like this:

if(System.Diagnostics.Debugger.IsAttached)
  System.Diagnostics.Debugger.Break();



当然,这仍然会在一个发布版本被编译中。如果你想让它更像调试对象,其中的代码只是一个发布版本不存在,那么你可以做这样的事情:

Of course that will still get compiled in a Release build. If you want it to behave more like the Debug object where the code simply doesn't exist in a Release build, then you could do something like this:

[Conditional("DEBUG")]
void DebugBreak()
{
  if(System.Diagnostics.Debugger.IsAttached)
    System.Diagnostics.Debugger.Break();
}



然后调用你的代码添加到它。

Then add a call to it in your code.

这篇关于可.NET源代码硬代码的调试断点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆