如何防止Debug.Assert的(...)来显示一个模式对话框 [英] How to prevent Debug.Assert(...) to show a modal dialog
问题描述
我有一对夫妇它们使用Debug.Assert的(...)库。我认为Debug.Assert的(......)都很好,我仍然希望他们来执行,但我不希望他们挡住我的应用程序的执行。理想情况下,我只想他们在某个地方记录下来。
I have a couple of libraries which use Debug.Assert(...). I think that the Debug.Assert(...) are fine and I still want them to execute, but I don't want them to block the execution of my application. Ideally, I would only like them to be logged somewhere.
既然我不能改变图书馆的代码(和我还是想在调试编译和运行断言),我如何防止Debug.Assert的(...)来显示一个模式对话框?
Given that I can't change the code of the libraries (and that I still want to compile in debug and run the assertion), how do I prevent Debug.Assert(...) to show a modal dialog?
另外,我想,以确保主当断言发生(相同的行为忽略按钮)计划继续进行。
In addition, I would like to make sure that the main program continues when an Assert occurs (same behaviour as the Ignore button).
谢谢!
推荐答案
我不会推荐它。问题是, Debug.Assert的
只应该被解雇,当你在你的代码中的错误的 的。如果你不理会他们还是不解决这些问题,那么你就做你的用户帮倒忙。如果,另一方面,你射击 Debug.Assert的
的事情上的不的错误,那么你还做你的用户帮倒忙(通过降低Debug.Assert的影响)。
I wouldn't recommend it. The problem is that Debug.Assert
is only supposed to be fired when you have bugs in your code. If you just ignore them or don't fix them, then you are doing your users a disservice. If, on the other hand, you're firing Debug.Assert
for things that aren't bugs, then you're also doing your users a disservice (by reducing the impact of Debug.Assert).
说了这么多,你的可以的禁用它。你需要做的第一件事是从的 Debug.Listeners 系列:
Having said that, you can disable it. The first thing you need to do is remove the default listener from the Debug.Listeners collection:
Debug.Listeners.Clear();
然后,添加自己的,而不是:
Then, add your own instead:
Debug.Listeners.Add(new MyTraceListener());
您需要创建一个从的的TraceListener :
class MyTraceListener : TraceListener
{
// ...
public override void Fail(string msg, string detailedMsg)
{
// log the message (don't display a MessageBox)
}
}
的
重要的方法是的TraceListener .Fail 方法,它在执行 DefaultTraceListener
是显示消息框。
The important method is the TraceListener.Fail method, which in the implementation of DefaultTraceListener
is what displays the message box.
这篇关于如何防止Debug.Assert的(...)来显示一个模式对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!