Debug.Assert seems not to work in Mono

2019-04-21 15:34发布

问题:

Consider the following C# program:

using System;
using System.Diagnostics;

namespace Test
{
        class MainClass
    {
        public static void Main (string[] args)
        {
            Debug.Assert(false);
            Debug.Fail("fail!");
            Console.WriteLine ("Hello World!");
        }
    }
}

When compiling this using:

dmcs -debug -d:DEBUG Main.cs

and then running it with:

mono --debug Main.exe

the assertion and fail seem to be ignored. The output is just:

Hello World!

I checked other related questions on StackOverflow, but I could not find a solution. In particular the solution give in Mono - Debug.Assert does not work does not work. (UPDATE: the updated solution does work, see below comments.)

I use Mono 2.10.5-1 on Ubuntu 11.10.

回答1:

C# on mono - http://ebsteblog.wordpress.com/2009/05/06/debugassert-and-mono/

Excerpt from the article:

...if you create a .config file for your app and set the assertuienabled attribute to true, you get the same dialog as with .NET... File app.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.diagnostics>
        <assert assertuienabled="true" />
    </system.diagnostics>
</configuration>

Old answer: C++ comment if you did not specify -define DEBUG on command line/compile options.

For debug add

#define DEBUG

at the beginning of the code or

#define TRACE

for trace.

See the solution here: http://lists.ximian.com/pipermail/mono-list/2006-December/033774.html

p.s: I tried this with C++ not C#. This may not work for C#.



回答2:

You can use the xml configuration, or you can place it under the control of your program by adding a trace listener at runtime:

var tl = new System.Diagnostics.ConsoleTraceListener();
System.Diagnostics.Debug.Listeners.Add ( tl );

This has the added advantage of you being able to enable it after the program has started.



标签: c# mono