将控制台重定向到app.config中的Visual Studio调试输出窗口
-
05-07-2019 - |
题
我想让我的 Console.WriteLine()
命令出现在我的“输出”中。我的 Debug.WriteLine()
语句的窗口。我想我曾经想过如何做到这一点,但我不记得/在谷歌上找到如何再做一次。我似乎记得能够在 app.config
文件中执行此操作。
我找到了很多关于如何让控制台和调试语句出现在控制台输出中的说明,但没有找到如何让它们出现在“输出”中的说明。窗口。
有可能吗?
解决方案
基本上最简单的解决方案就是这样。
public class ToDebugWriter : StringWriter
{
public override void WriteLine(string value)
{
Debug.WriteLine(value);
base.WriteLine(value);
}
}
您必须将此行添加到程序的初始化中:
Console.SetOut(new ToDebugWriter());
其他提示
@ Avram的答案对我有用,除了他的代码中的单个重载不是log4net的那个 ConsoleAppender
正在我的系统上使用。 (我对 Console.SetOut
感兴趣,以便log4net的 ConsoleAppender
输出到Visual Studio的“Debug”输出窗格。)所以我覆盖了所有 StringWriter 代码>的
Write
和 WriteLine
方法接受 string
, object
, char []
等等,假设其中一个或多个是 ConsoleAppender
通过 Console
调用的。
这成功了,现在log4net日志记录出现在我的“Debug”中。窗格。
我将以下代码包含在内,以便为具有类似目标的任何人提供帮助。 (为了完全安全,可以覆盖剩余的 StringWriter.Write
和 .WriteLine
方法。)我已经删除了对 base
的调用,因为它们似乎是不必要的,我认为它们只是在 StringWriter
中构建一个大缓冲区(通常通过该类的 .ToString()
访问。)
namespace GeneralLibrary.Logging
{
using System.Diagnostics;
using System.IO;
public class DebugWriter : StringWriter
{
public override void Write(string format, object arg0)
{
Debug.Write(string.Format(format, arg0));
}
public override void Write(string format, object arg0, object arg1)
{
Debug.Write(string.Format(format, arg0, arg1));
}
public override void Write(string format, object arg0, object arg1, object arg2)
{
Debug.Write(string.Format(format, arg0, arg1, arg2));
}
public override void Write(string format, params object[] arg)
{
Debug.Write(string.Format(format, arg));
}
public override void Write(object value)
{
Debug.Write(value);
}
public override void Write(string value)
{
Debug.Write(value);
}
public override void Write(char[] buffer)
{
Debug.Write(buffer);
}
public override void Write(char[] buffer, int index, int count)
{
Debug.Write(new string(buffer, index, count));
}
public override void WriteLine(string value)
{
Debug.WriteLine(value);
}
public override void WriteLine(object value)
{
Debug.WriteLine(value);
}
public override void WriteLine(string format, object arg0)
{
Debug.WriteLine(format, arg0);
}
public override void WriteLine(string format, object arg0, object arg1)
{
Debug.WriteLine(format, arg0, arg1);
}
public override void WriteLine(string format, object arg0, object arg1, object arg2)
{
Debug.WriteLine(format, arg0, arg1, arg2);
}
public override void WriteLine(string format, params object[] arg)
{
Debug.WriteLine(format, arg);
}
public override void WriteLine(char[] buffer)
{
Debug.WriteLine(buffer);
}
public override void WriteLine(char[] buffer, int index, int count)
{
Debug.WriteLine(new string(buffer, index, count));
}
public override void WriteLine()
{
Debug.WriteLine(string.Empty);
}
}
}
如果您可以获取输出窗口的流,则可以使用 Console.SetOut()重定向到它。然而,这种方法似乎不可能。
System.Debug 输出到每个< a href =“http://msdn.microsoft.com/en-us/library/system.diagnostics.tracelistener.aspx”rel =“nofollow noreferrer”> TraceListener 在其 TraceListenerCollection 。最初只注册了一个TraceListener,它是 DefaultTraceListener 。它不使用流对象,而是使用本机方法进行输出。
使用Visual Studio API的方法可能就是这样。