似乎.net核心中不存在Debug.Listeners

时间:2019-01-24 08:51:01

标签: c# .net-core

net core2.2中似乎不存在 Debug.Listeners

在.net框架中,我可以使用它:

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

然后我在调试时可以在控制台中看到调试消息。
但是它不能在网络核心中工作,它将给出错误消息,例如“调试不包含侦听器”。我使用F12搜索(dotnet核心):

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);
}

是的,我找不到它,至少它不是公开的。
如何像以前一样调试应用程序?

官方文档(docs.microsoft.com)表示跟踪和调试共享侦听器,但是我的测试结果是:

        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();

,该示例使用Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));,但是Debug.Listeners不存在。

2 个答案:

答案 0 :(得分:1)

看来我无法解决该问题,但是我可以像这样调试我的应用程序:

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

只需创建一个新方法,然后添加[System.Diagnostics.ConditionalAttribute("DEBUG")]

答案 1 :(得分:1)

从.NET Core 3.0开始,您可以改为使用Trace.Listeners。它也会影响Debug,并且在功能上是等效的。