Rob*_*ker 70 c# breakpoints visual-studio .net-2.0
我正在寻找.NET中的一种方法(特别是2.0,C#),以便触发调试中断,就像在该点设置断点一样,而不必记住在调试器中设置特定的断点,并且不干扰生产运行时.
我们的代码需要在生产中吞下异常,所以我们不会破坏链接到我们的客户端应用程序,但是我正在尝试将其设置为如果它恰好在调试器中运行时会弹出这样的错误进行分析,否则将被安全地忽略.
我的使用尝试Debug.Assert(false)
不太理想,我认为这样做Debug.Fail()
会有相同的表现.理论上它应该在生产中没有效果,并且在调试时它确实成功停止,但是根据设计(如我所知)如果你想忽略该错误就没有办法继续执行,就像你可以使用实际的断点一样,就像我们吞下错误的生产一样.它显然也打破了变量状态的评估,因为调试器实际上在本机系统代码中停止而不是在我们的代码中,所以它的调试帮助是有限的.(也许我错过了某些方法来重新审视变量等等.它发生的地方.???)
我希望有类似的东西Debug.Break()
,但它似乎不存在(除非可能在.NET的更高版本中?),也没有其他Debug
方法似乎适用.
更新:虽然ctacke的答案是我所寻找的最佳匹配,但我还发现了Debug.Assert()的一个技巧 - 在调试器中运行时 - 暂停调试器,转到Debug的代码.断言呼叫挂起(以绿色突出显示,因为它在框架代码中已关闭)并按下Step-Out(shift-F11),然后在断言对话框中点击Ignore.这将使调试器在断言返回时暂停(并且能够继续执行,就像它没有发生一样,因为它被忽略了).可能有其他方法可以做同样的事情(点击重试更直接吗?),但这种方式很直观.
cta*_*cke 127
你可能喜欢这样的事情:
if(System.Diagnostics.Debugger.IsAttached)
System.Diagnostics.Debugger.Break();
Run Code Online (Sandbox Code Playgroud)
当然,它仍将在Release版本中编译.如果你希望它的行为更像Debug对象,其中代码根本不存在于Release版本中,那么你可以这样做:
// Conditional("Debug") means that calls to DebugBreak will only be
// compiled when Debug is defined. DebugBreak will still be compiled
// even in release mode, but the #if eliminates the code within it.
// DebuggerHidden is so that, when the break happens, the call stack
// is at the caller rather than inside of DebugBreak.
[DebuggerHidden]
[Conditional("DEBUG")]
void DebugBreak()
{
if(System.Diagnostics.Debugger.IsAttached)
System.Diagnostics.Debugger.Break();
}
Run Code Online (Sandbox Code Playgroud)
然后在代码中添加对它的调用.
Ser*_*gey 10
如果你想只有一行代码而不是4行,请换行
#if DEBUG
if (Debugger.IsAttached)
Debugger.Break();
#endif
Run Code Online (Sandbox Code Playgroud)
成
public static class DebugHelper
{
[DebuggerHidden]
[Conditional("DEBUG")]
public static void Stop()
{
if (Debugger.IsAttached)
Debugger.Break();
}
}
Run Code Online (Sandbox Code Playgroud)
并使用
DebugHelper.Stop();
Run Code Online (Sandbox Code Playgroud)
DebuggerHiddenAttribute
已被添加,以防止调试器停止在方法的内部代码Stop
和步入方法与F11
.
我遇到过一次无法正常工作的情况
System.Diagnostics.Debugger.Break();
Run Code Online (Sandbox Code Playgroud)
但这样做了
System.Diagnostics.Debugger.Launch();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26873 次 |
最近记录: |