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.
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.
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.
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.
NET Framework Class Library namespace System.
As of .NET Core 3.0, you can use Trace.Listeners
instead. It affects Debug
too and is functionally equivalent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With