Martin
Martin

Reputation: 40573

How to prevent Debug.Assert(...) to show a modal dialog

I have a couple of libraries which use Debug.Assert(...). I think that the Debug.Assert(...) are fine and I still want them to execute, but I don't want them to block the execution of my application. Ideally, I would only like them to be logged somewhere.

Given that I can't change the code of the libraries (and that I still want to compile in debug and run the assertion), how do I prevent Debug.Assert(...) to show a modal dialog?

In addition, I would like to make sure that the main program continues when an Assert occurs (same behavior as the Ignore button).

Thanks!

Upvotes: 31

Views: 13154

Answers (4)

BaiJiFeiLong
BaiJiFeiLong

Reputation: 4605

Maybe someone want to display a dialog but not, in latest version of DotNet

Here is an complete example, you can enable to disable it by yourself

// Created By [email protected] at 2024-11-17 17:23:56+0800

using System.Diagnostics;

Application.SetCompatibleTextRenderingDefault(false);
Application.EnableVisualStyles();
Application.SetHighDpiMode(HighDpiMode.SystemAware);

Trace.Listeners.Clear();
Trace.Listeners.Add(new MyTraceListener());

var form = new Form();
form.ClientSize = new Size(800, 450);
var button = new Button { Dock = DockStyle.Fill, Text = "CLICK ME" };
button.Click += delegate { Trace.Assert("Hello".StartsWith("World"), "Hello Should Starts With World!"); };
form.Controls.Add(button);
Application.Run(form);

internal class MyTraceListener : DefaultTraceListener
{
    public override void Fail(string? message, string? detailMessage)
    {
        var stackTrace = new StackTrace(true).ToString();
        var combinedMessage = $"{message}\n{detailMessage}".Trim();
        var finalMessage = $"{combinedMessage}\n{stackTrace}".Trim();
        var form = new Form();
        form.StartPosition = FormStartPosition.CenterScreen;
        form.ClientSize = new Size(800, 450);
        form.Text = "Assertion Failed!";
        form.Icon = SystemIcons.Error;
        var label = new Label { Dock = DockStyle.Fill };
        label.Text = finalMessage;
        form.Controls.Add(label);
        form.ShowDialog();
    }
}

Upvotes: 0

Matt Ellis
Matt Ellis

Reputation: 1069

Codekas answer is correct, if you want to hit things with a really big hammer. You can use the <assert> element in your application configuration file to set assertuienabled property to false and optionally give a log file for Asserts to be written to. Then you won't have to write your own listener.

You can read more about the assert element at its MSDN page.

Upvotes: 5

Julian
Julian

Reputation: 36710

There is no need for Debug.Listeners.Clear()

Just add to your .config:

<system.diagnostics>
    <assert assertuienabled="false"/>
</system.diagnostics>

Upvotes: 33

Dean Harding
Dean Harding

Reputation: 72658

I wouldn't recommend it. The problem is that Debug.Assert is only supposed to be fired when you have bugs in your code. If you just ignore them or don't fix them, then you are doing your users a disservice. If, on the other hand, you're firing Debug.Assert for things that aren't bugs, then you're also doing your users a disservice (by reducing the impact of Debug.Assert).

Having said that, you can disable it. The first thing you need to do is remove the default listener from the Debug.Listeners collection:

Debug.Listeners.Clear();

Then, add your own instead:

Debug.Listeners.Add(new MyTraceListener());

You need to create a class that inherits from TraceListener:

class MyTraceListener : TraceListener
{
    // ...

    public override void Fail(string msg, string detailedMsg)
    {
        // log the message (don't display a MessageBox)
    }
}

The important method is the TraceListener.Fail method, which in the implementation of DefaultTraceListener is what displays the message box.

Upvotes: 33

Related Questions