Causing VS2010 debugger to break when Debug.Assert fails

后端 未结 7 2077
南笙
南笙 2020-12-15 09:15

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

相关标签:
7条回答
  • 2020-12-15 09:33

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

    0 讨论(0)
  • 2020-12-15 09:33

    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.

    0 讨论(0)
  • 2020-12-15 09:42

    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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 09:46

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

    0 讨论(0)
  • 2020-12-15 09:48

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

    0 讨论(0)
  • 2020-12-15 09:50

    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.

    0 讨论(0)
提交回复
热议问题