user128300
user128300

Reputation:

Causing VS2010 debugger to break when Debug.Assert fails

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 code I have lines like this:

Debug.Assert(!double.IsInfinity(x));

If I am not debugging, a window pops up when the assertion fails.

But when I am debugging, the assertion is logged to the "Output" pane, which is easy to miss; there is not popup window and the debugger does not stop. Therefore: is there any way to force the Visual Studio debugger to break when Debug.Assert fails?

(BTW: I am developing a WPF based desktop application. In a Windows Forms application, the behavior seems to be differnt: here, the debugger stops on Debug.Assert.)

EDIT: Please let me clarify: I am not looking for an alternative to Debug.Assert(), because my code and external code I use is full of Debug.Assert() statements. I am looking for a way to cause the Visual Studio debugger to break when Debugg.Assert fails. (I think earlier VS versions did that, and that the behavior changed in VS2010).

Upvotes: 12

Views: 5851

Answers (7)

participant
participant

Reputation: 3003

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.

Upvotes: 0

Joe Enos
Joe Enos

Reputation: 40393

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.

Upvotes: 0

user
user

Reputation: 6947

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();
        }
    }
}

Upvotes: 1

František Žiačik
František Žiačik

Reputation: 7614

If by debugging you mean using "Step Into" feature, see this MS's answer to the problem.

http://connect.microsoft.com/VisualStudio/feedback/details/522995/system-diagnostics-debug-assert-doesnt-pop-up-messagebox-correctly-when-step-into-it-in-vs-2010-and-2008

Using "Step Over" at the Assert statement does solve the issue too (for me).

Upvotes: 7

Martin Liversage
Martin Liversage

Reputation: 106826

Use the Debugger.Break method (in the System.Diagnostics namespace). This will break execution if you are running in a debugger.

Upvotes: 1

Alexander
Alexander

Reputation: 1297

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))

Upvotes: 1

&#216;yvind Br&#229;then
&#216;yvind Br&#229;then

Reputation: 60694

Can't you just throw an exception when !double.IsInfinity(x) instead of using an assertion?

Upvotes: 0

Related Questions