Debug.Assert(false) does not trigger in win8 Metro

2019-04-22 22:32发布

问题:

I notice Debug.Assert does not trigger in Metro apps, however, if the project is a traditional one like Console or WinForm, it does trigger. And yes, I am in Debug mode.

Is it a setting not properly set in Visual Studio (11 Beta)? Or Debug.Assert is intended to be disabled in metro apps?

I know many exceptions are swallowed during the execution of Metro apps, but Debug.Assert is so handy that I can't think of a reason why it should be disabled.

回答1:

It does trigger, look in the Output window. It just doesn't automatically prompt you to ask if you want a debugger break and thus just keeps motoring.

The DefaultTraceListener.AssertUIEnabled property is false. That's an implementation problem, can't display a message box on top of Metro UI. Which does actually work but the monitor switches to the desktop, pretty undesirable when you would have liked to click No. Hard to solve and no doubt on the todo list. You can't easily get to the property to set it to true, it is inaccessible from the metadata. Filip's workaround sounds half-decent.



回答2:

Seems like a bug. I would roll out my own assert method. Something like:

[Conditional("DEBUG")]
public static void Assert(bool condition)
{
    if (!condition)
        System.Diagnostics.Debugger.Break();
}


回答3:

There is the same problem with F# in WinRT, in VS2013. The assert statement, which is an alias for System.Diagnostics.Debug.Assert, does not raise an exception, so unless you are watching the Output window then your assertions can fail without being noticed. Even if you are watching, it is hard to find the spot where the assertion was raised.

I followed Filip's suggestion and wrote a short utility, as follows:

namespace MyProj.Infrastructure

module Diagnostics =

    let Assert condition = if not condition then
                                System.Diagnostics.Debugger.Break()

I chose Debugger.Break over raising an exception because it stops the debugger at the place the assertion fails. However, raising an exception is an acceptable alternative.

I didn't have any suitable global projects or modules already in my solution, so I had to create them just for this, which was quite annoying.