Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It seems that Debug.Listeners does not exist in .net core

Tags:

c#

.net-core

It seems that Debug.Listeners does not exists in net core2.2

In .net framework, I can use this:

        Debug.Assert(true);
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        Debug.WriteLine("Debug");

then I can see my debug messages in the console when I debug.
But it can't work in net core, it will give error message like that "Debug does not contain Listeners". I use F12 to search(dotnet core):

public static class Debug
{
    public static int IndentSize { get; set; }
    public static bool AutoFlush { get; set; }
    public static int IndentLevel { get; set; }
    public static void Assert(bool condition);
    public static void Assert(bool condition, string message);
    public static void Assert(bool condition, string message, string detailMessageFormat, params object        public static void Assert(bool condition, string message, string detailMessage);
    public static void Close();
    public static void Fail(string message);
    public static void Fail(string message, string detailMessage);
    public static void Flush();
    public static void Indent();
    public static void Print(string message);
    public static void Print(string format, params object        public static void Unindent();
    public static void Write(string message, string category);
    public static void Write(object value, string category);
    public static void Write(object value);
    public static void Write(string message);
    public static void WriteIf(bool condition, object value);
    public static void WriteIf(bool condition, string message);
    public static void WriteIf(bool condition, string message, string category);
    public static void WriteIf(bool condition, object value, string category);
    public static void WriteLine(object value);
    public static void WriteLine(object value, string category);
    public static void WriteLine(string message);
    public static void WriteLine(string format, params object        public static void WriteLine(string message, string category);
    public static void WriteLineIf(bool condition, object value);
    public static void WriteLineIf(bool condition, object value, string category);
    public static void WriteLineIf(bool condition, string message);
    public static void WriteLineIf(bool condition, string message, string category);
}

It is true I can't find it, at least it is not public.
How can I debug my application just like before?

The offical document(docs.microsoft.com) says that Trace and Debug share the Listeners, but my test result is:

        TextWriterTraceListener myWriter = new TextWriterTraceListener(Console.Out);
        //Debug.Assert(false);    //Assertion Failed
        Trace.Listeners.Add(myWriter);
        Debug.AutoFlush = true;
        Debug.Indent();
        Trace.WriteLine("Trace");   //write Trace
        Debug.WriteLine("Debug");   //Don't write Debug
        Console.ReadLine();

and the example use Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));, but the Debug.Listeners does not exist.

like image 796
Wei Avatar asked Jan 24 '19 08:01

Wei


People also ask

How do you debug a listener?

Right click > inspect element # On the page, right-click the element you want to debug event listeners for, then click Inspect Element. In chromium-based browsers like MS Edge and Google Chrome, click the Event Listeners tab in Developer Tools.

How do I debug a .NET core application?

Open the Debug view by selecting the Debugging icon on the left side menu. Select the green arrow at the top of the pane, next to . NET Core Launch (console). Other ways to start the program in debugging mode are by pressing F5 or choosing Run > Start Debugging from the menu.

What is the difference between debug write and trace write in C#?

Debug. Write is only effective on builds where the DEBUG flag is defined, while Trace. Write is only effective when the TRACE flag is defined. Save this answer.

Which namespace should be used for generating the output in debug window?

NET Framework Class Library namespace System.


1 Answers

As of .NET Core 3.0, you can use Trace.Listeners instead. It affects Debug too and is functionally equivalent.

like image 100
Pathogen David Avatar answered Oct 17 '22 04:10

Pathogen David