将控制台重定向到 app.config 中的 Visual Studio 调试输出窗口
我想让我的 Console.WriteLine()
命令与我的 Debug.WriteLine()
语句一起出现在我的“输出”窗口中。我想我曾经弄清楚如何做到这一点,但我不记得/在谷歌上找到如何再次做到这一点。我似乎记得能够在 app.config
文件中执行此操作。
我找到了大量关于如何使控制台和调试语句出现在控制台输出中的说明,但没有找到如何使它们出现在“输出”窗口中的说明。
是否可以?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本上最简单的解决方案如下所示。
并且您必须将此行添加到程序的初始化中:
Basically the most simple solution looks like this.
And you must add this line to the initialization of the program:
@Avram 的答案 对我有用,只是他的代码中的单个重载不是 log4net 的
的重载ConsoleAppender
在我的系统上使用。 (我对Console.SetOut
感兴趣,以便 log4net 的ConsoleAppender
输出到 Visual Studio 的“调试”输出窗格。)因此我覆盖了所有StringWriter
> 的Write
和WriteLine
方法接受string
、object
、char[]
等等,假设其中一个或多个是ConsoleAppender
通过Console
调用的。这成功了,log4net 日志记录现在出现在我的“调试”窗格中。
我添加了下面的代码,以便任何具有类似目标的人受益。 (为了完全安全,可以重写其余的
StringWriter.Write
和.WriteLine
方法。)我删除了对base
的调用,因为它们似乎是不必要的,我认为它们只是在 StringWriter 内部构建了一个大缓冲区(通常通过该类的 .ToString() 访问。)@Avram's answer has worked for me, except that the single overload in his code wasn't the one that log4net's
ConsoleAppender
was using on my system. (I'm interested inConsole.SetOut
so that log4net'sConsoleAppender
outputs to Visual Studio's "Debug" output pane.) So I overrode all ofStringWriter
'sWrite
andWriteLine
methods acceptingstring
,object
,char[]
, etc. on the assumption that one or more of these was whatConsoleAppender
was calling viaConsole
.This succeeded, and log4net logging now appears in my "Debug" pane.
I'm including the code below for the benefit of anyone with similar goals. (To be entirely safe, one could override the remaining
StringWriter.Write
and.WriteLine
methods.) I've removed the calls tobase
because they appear to be unnecessary, and I think they just build up a large buffer insideStringWriter
(usually accessed via that class's.ToString()
.)如果您可以获取输出窗口的流,则可以使用 Console.SetOut() 重定向到它。然而这种方法似乎不可能。
System.Debug 输出到每个 TraceListener 位于其 TraceListenerCollection。最初只注册了一个 TraceListener,即 DefaultTraceListener。它不使用流对象,而是使用本机方法进行输出。
使用 Visual Studio API 的方法可能是最佳选择。
If you can get hold of the stream for the output window you can use Console.SetOut() to redirect to it. However this approach doesn't appear to be possible.
System.Debug outputs to every TraceListener in its TraceListenerCollection. There is only one TraceListener registered initially which is the DefaultTraceListener. It does not make use of a stream object and instead uses native methods for output.
An approach that uses the Visual Studio API is probably the way to go.