Is there any way to cause Visual Studio 2010 to break while debugging when the argument of Debug.Assert
evaluates to false
?
Example: in my
Can't you just throw an exception when !double.IsInfinity(x)
instead of using an assertion?
If using the DefaultTraceListener
you can add the following section to your app.config
file which enables breaking on Debug.Assert(false)
:
<configuration>
<system.diagnostics>
<assert assertuienabled="false"/>
</system.diagnostics>
</configuration>
For details see assert element and DefaultTraceListener.AssertUiEnabled Property.
It seems to work as expected for me in a console application at least: in debug builds, a dialog pops up that allows you to break into the application using the debugger, but in release builds, nothing happens. Admittedly, I just used a simplistic Debug.Assert(false);
, but I don't see why that should make much of a difference. I'm running Visual Studio 2010 SP1 Ultimate.
I'd suggest that you take a close look at your build settings.
Here's the code I used:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Debug.Assert(false);
Console.ReadLine();
}
}
}
Use the Debugger.Break method (in the System.Diagnostics
namespace). This will break execution if you are running in a debugger.
You can use condition breakpoint for this sort of behavior, just set a breakpoint on a line on which you want to break and then right click on the point at the left and choose condition, in popup enter desired condition (in your case is double.IsInfinity(x))
It appears that your app removed the default TraceListener from the Debug class, and replaced it with a custom one that implements the Fail(string)
method differently.
You may be able to track it down by searching your code for Debug.Listeners
and see if somewhere the default one is cleared out or modified, or see if you've got a trace
section in your app.config.
By default, from what I've read, you should definitely get the popup window.