Wei
Wei

Reputation: 561

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.

Upvotes: 29

Views: 6576

Answers (4)

Wei
Wei

Reputation: 561

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")].

Upvotes: 1

HackTheDj
HackTheDj

Reputation: 1

Replace the default Trace Listener

AppSettings:

"Debug": {
  "DebugFile": ".\\Debug{Date}.log"
},

Code:

string debugLocation = (string)configurationRoot.GetSection("Debug").GetValue("".GetType(),"DebugFile");
debugLocation = debugLocation.Replace("{Date}", DateTime.Today.ToString("yyyyMMdd"));
var deft = new DefaultTraceListener();
deft.LogFileName = debugLocation;
deft.IndentLevel = 0;
deft.IndentSize = 4;
Trace.Listeners[0] = deft;
Debug.AutoFlush = true;
Debug.WriteLine("----------- "+DateTime.Now.ToString()+ " Application Starting -----------");

Upvotes: 0

Fritz W
Fritz W

Reputation: 61

ConsoleTraceListener works just like System.Diagnostics.Debug.Listeners in .Net before .Net Core like Pathogen David said.

And then very importantly set the Windows application to a Console Application in the Applications Properties page under General->Output type. This is launching the Console window as well as the default form.

I've added this code to main.cs in Main():

...
var myWriter = new ConsoleTraceListener();
Trace.Listeners.Add(myWriter);
...

Then

// Console Logging
Console.Writeline("Your Text here will then be send to the Console");

Upvotes: 0

Pathogen David
Pathogen David

Reputation: 886

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

Upvotes: 24

Related Questions