Qui*_*son 11 console-application visual-studio
我想让我的Console.WriteLine()命令出现在我的"输出"窗口中,带有我的Debug.WriteLine()语句.我想我想出了如何做到这一次,但我不记得/在谷歌上找到如何再次这样做.我似乎记得能够做到这一点app.config
我找到了很多关于如何打赌控制台和调试语句出现在控制台输出中的说明,但没有找到如何让它们出现在"输出"窗口中.
有人知道吗?
Avr*_*ram 19
基本上最简单的解决方案看起来像这样.
public class ToDebugWriter : StringWriter
{
public override void WriteLine(string value)
{
Debug.WriteLine(value);
base.WriteLine(value);
}
}
Run Code Online (Sandbox Code Playgroud)
并且你必须在程序初始化中添加这行"Console.SetOut(new ToDebugWriter());"
@Avram 的答案对我有用,只是他的代码中的单个重载不是 log4netConsoleAppender在我的系统上使用的重载。(我感兴趣的Console.SetOut是 log4net 的ConsoleAppender输出到 Visual Studio 的“调试”输出窗格。)因此,我覆盖了所有StringWriter接受、、等Write的WriteLine方法,假设其中一个或多个是通过 调用的。stringobjectchar[]ConsoleAppenderConsole
这成功了,log4net 日志记录现在出现在我的“调试”窗格中。
我添加了下面的代码,以便任何具有类似目标的人受益。(为了完全安全,可以重写剩余的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6792 次 |
| 最近记录: |