简体   繁体   English

Debug.WriteLine仅显示在“输出”窗口中,而不显示在“控制台”窗口中

[英]Debug.WriteLine shows up only in Output window and not Console window

I am trying to clear up what gets written in the console when not debugging so for example when running in release it will not show up. 我试图清除未调试时在控制台中写入的内容,例如在发行版中运行时将不会显示。 So for that i use this method for the Logging that should not be shown 因此,为此,我将这种方法用于不应显示的日志记录

    public static void DebugWriteConsole(string s)
    {
        Debug.WriteLine(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss | ") + s);
        if (Log.logger != null)
        {
            Log.Info(s);
        }
    }

And it works in that regard that it doesn't show up when running in release but the problem i have is that i run the application with -c so it runs in a console window but when running in debug the Debug.WritLine only prints into the vs output window and nothing in the console window. 它在这方面起作用,在发行版中运行时不会显示,但是我的问题是我使用-c运行应用程序,因此它在控制台窗口中运行,但是在调试中运行时, Debug.WritLine仅打印到vs输出窗口,而控制台窗口中没有任何内容。 Anyone know how to solve this? 有人知道如何解决吗?

Has been explained in the Microsoft Docs in the TraceListeners collection topic 在Microsoft文档的TraceListeners集合主题中已进行了解释

You can use TextWriterTraceListener and specify the System.Console.Out as the stream where you want to write (or any other suitable stream instance) 您可以使用TextWriterTraceListener并将System.Console.Out指定为要写入的流(或任何其他合适的流实例)

TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(myWriter);

or just use ConsoleTraceListener. 或仅使用ConsoleTraceListener。

ConsoleTraceListener trc = new ConsoleTraceListener();
Debug.Listeners.Add(trc);

Another option is to use a pragma directive to overcome the NET Core problem 另一个选择是使用编译指示来克服NET Core问题

public static void DebugWriteConsole(string s)
{
    #if DEBUG
        Console.WriteLine(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss | ") + s);
    #endif
    Debug.WriteLine(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss | ") + s);
    if (Log.logger != null)
    {
        Log.Info(s);
    }
}

Steve way will probably work for most but if you are using .Net Core and have this problem i found a solution like this since .Net Core doesn't have Debug.Listeners A way to do this is by writing the method likes this 史蒂夫(Steve)的方法可能适用于大多数情况,但是如果您使用的是.Net Core并且遇到此问题,我发现了这样的解决方案,因为.Net Core没有Debug.Listeners

[Conditional("DEBUG")]
    public static void DebugWriteConsole(string s)
    {
        Console.WriteLine(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss | ") + s);
        if (Log.logger != null)
        {
            Log.Info(s);
        }
    }

still using the Console.WriteLine but putting the Conditional attribute on the method 仍然使用Console.WriteLine但将Conditional属性放在方法上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Debug.WriteLine如何将消息发送到输出窗口 - How does Debug.WriteLine send messages to output window 如何从执行Debug.WriteLine的VS 2015输出窗口行跳转到代码? - How to jump to the code from the VS 2015 Output window line where a Debug.WriteLine was executed? 的Debug.WriteLine - Debug.Writeline Debug.WriteLine 命令没有出现任何输出 - Debug.WriteLine command does not appear any output Debug.WriteLine output 没有显示应用程序中的所有进程 - Debug.WriteLine output no showing for all processes in an application Release.WriteLine来自发布模式下的日志程序集,而不是写入Visual Studio中的调试输出 - Debug.WriteLine from Logging Assembly in Release Mode not writing to debug output in Visual Studio 如何在 Azure 应用服务网站上获取 Debug.WriteLine 数据(或类似输出)? - How can I get Debug.WriteLine data (or similar output) on an Azure App Services website? 如何在输出窗口中过滤System.Diagnostics.Debug.WriteLine? - How Do I filter System.Diagnostics.Debug.WriteLine in output window? C# 中的 Debug.WriteLine() - 它有什么作用? - Debug.WriteLine() in C# - What does it do? Debug.WriteLine()在运行测试时发生两次 - Debug.WriteLine() happening twice when running tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM