Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 310
Adrian Grigore Avatar asked Dec 29 '09 17:12

Adrian Grigore


People also ask

How does a debug assert work?

Assert is typically used when debugging to test an expression that should evaluate to True. If it doesn't, the Immediate window can be used to investigate why the test failed. Debug. Assert executes only when an application is run in the design-time environment; the statement has no effect in a compiled application.

Does debug assert work in Release mode?

Assert works only in DEBUG mode - or, at least, when the DEBUG variable is defined. Otherwise, all those checks will simply get removed from the build result, so they will not impact your application when running in RELEASE mode.

Can asserts help with debugging?

Assertions are a convenient tool for documenting, debugging, and testing code during development. Once you've debugged and tested your code with the help of assertions, then you can turn them off to optimize the code for production. Assertions will help you make your code more efficient, robust, and reliable.


2 Answers

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.

like image 158
user275587 Avatar answered Oct 03 '22 05:10

user275587


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.

like image 25
rism Avatar answered Oct 03 '22 04:10

rism