[英]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.