Debug.Assert() has stopped working in my project

For some reason, the following line does nothing in my ASP.NET MVC project:

  System.Diagnostics.Debug.Assert(false);

I have triple-checked that I am using the Debug configuration and "Define Debug constant" is checked in the Debug configuration settings.

The same problem also occurs in my unit test project.

Implementing my own assert method seems trivial, but a bit awkward. Any hints on how to fix this would be greatly appreciated.

Edit: I am using several third-party modules in my project. Could this perhaps be caused by referencing a module which is compiled in release mode?


ASP.Net Assertion are displayed in the VS Console while your webpage is displayed through VisualStudio. It doesn't interrupt the thread to display a MsgBox or break to the assertion line like a programming language.


Ancient question but if you don't have a default listener defined it will not display a message dialog as per usual. I haven't confirmed whether it actually fires and just gets eaten (I suspect this is case) or whether it just doesn't fire at all.

But either way it will not show the dialog.

From the docs for DefaultTraceListener

The display of the message box for Assert and Fail method calls depends on the presence of the DefaultTraceListener. If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed.

The DefaultTraceListener can be removed by the element, by the element, or by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()).

You can check your listeners and get the type by using some code like below:

 var listeners = new TraceListener[Debug.Listeners.Count];
 Debug.Listeners.CopyTo(listeners, 0);
 foreach (var listener in listeners) {
    Debug.WriteLine("Name : {0} of type : {1}", listener.Name, listener.GetType());
 }

If you don't have one called "Default", Debug.Assert will silently fail.

As far as configuration goes, this WILL work assuming a listener named Default is available:

<system.diagnostics>
    <trace autoflush="false">
        <listeners>
        </listeners>
    </trace>
</system.diagnostics>

This WILL work assuming a listener named Default is available:

<system.diagnostics>
    <trace autoflush="false">
        <listeners>
           <add name="bigEarsListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TracingInfo.log"/>
        </listeners>
    </trace>
</system.diagnostics>

This WILL work as we explictly define our Default:

<system.diagnostics>
    <trace autoflush="false">
        <listeners>
           <remove name="Default" />
           <add name="Default" type="System.Diagnostics.DefaultTraceListener" />
           <add name="bigEarsListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TracingInfo.log"/>
        </listeners>
    </trace>
</system.diagnostics>

This WONT work:

<system.diagnostics>
    <trace autoflush="false">
        <listeners>
           <remove name="Default" />
           <add name="bigEarsListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TracingInfo.log"/>
        </listeners>
    </trace>
</system.diagnostics>

If you don't have a diagnostics section in your web.config then the Default might be getting removed or overriden by some VS Extension etc, so adding this section should bring it back to expected behaviour.


既然你正在运行ASP.NET MVC,那么你的web.config中是否会有一个debug = false导致问题?

链接地址: http://www.djcxy.com/p/27954.html

上一篇: C#发布版本仍然有.pdb文件

下一篇: Debug.Assert()已停止在我的项目中工作