Search code examples
c#.net-core

It seems that Debug.Listeners does not exist in .NET Core


It seems that Debug.Listeners does not exists in .NET Core 2.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 this: "Debug does not contain Listeners". I use F12 to search (.NET 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 official document(Debug) 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 uses Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));, but the Debug.Listeners does not exist.


Solution

  • It seems that I don't have any way to solve that problem, but I can debug my application like this:

    public static void a()
    {
        Console.WriteLine("always show");
        DebugLog();
    }
    
    [System.Diagnostics.ConditionalAttribute("DEBUG")]
    static void DebugLog()
    {
        Console.WriteLine("debug show");
    }
    

    Just create a new method and then add [System.Diagnostics.ConditionalAttribute("DEBUG")].