Car*_*l R 19
控制台可以将其输出重定向到任何文本编写器.如果您实现了写入Diagnostics.Debug的文本编写器,那么您已经完成了设置.
这是一个写入调试器的文本编写器.
using System.Diagnostics;
using System.IO;
using System.Text;
namespace TestConsole
{
public class DebugTextWriter : TextWriter
{
public override Encoding Encoding
{
get { return Encoding.UTF8; }
}
//Required
public override void Write(char value)
{
Debug.Write(value);
}
//Added for efficiency
public override void Write(string value)
{
Debug.Write(value);
}
//Added for efficiency
public override void WriteLine(string value)
{
Debug.WriteLine(value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于它使用Diagnostics.Debug,它将遵循您的编译器设置,以便它应该写入任何输出.此输出也可以在Sysinternals DebugView中看到.
以下是您使用它的方式:
using System;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
Console.SetOut(new DebugTextWriter());
Console.WriteLine("This text goes to the Visual Studio output window.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果要在发布模式下编译时在Sysinternals DebugView中查看输出,可以使用写入OutputDebugString API的TextWriter.它可能看起来像这样:
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace TestConsole
{
public class OutputDebugStringTextWriter : TextWriter
{
[DllImport("kernel32.dll")]
static extern void OutputDebugString(string lpOutputString);
public override Encoding Encoding
{
get { return Encoding.UTF8; }
}
//Required
public override void Write(char value)
{
OutputDebugString(value.ToString());
}
//Added for efficiency
public override void Write(string value)
{
OutputDebugString(value);
}
//Added for efficiency
public override void WriteLine(string value)
{
OutputDebugString(value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
AMi*_*ico 10
NullStream,定义为"没有后备存储的流".所有方法都不做任何事情或什么都不返回 这是一个内部课程Stream.以下代码取自Microsoft的源代码.
基本上,当Console第一次调用其中一个write方法时,会调用Windows API函数GetStdHandle进行"标准输出".如果没有返回句柄,则NullStream创建并使用它.
塞缪尔的答案是正确的,并提供一般信息.要实际重定向控制台输出,无论项目类型如何,请使用Console.SetOut(New System.IO.StreamWriter("C:\ConsoleOutput.txt")),这是一个简单的示例.
直接回答你的问题.使用ConsoleTraceListener和a StreamWriter将所有三个输出定向到文件.我只使用以下内容进行开发.
Dim oLogFile As New System.IO.StreamWriter("C:\ConsoleOutput.txt")
oLogFile.AutoFlush = True 'so we do not have to worry about flushing before application exit
Console.SetOut(oLogFile)
'note, writing to debug and trace causes output on console, so you will get double output in log file
Dim oListener As New ConsoleTraceListener
Debug.Listeners.Add(oListener)
Trace.Listeners.Add(oListener)
Run Code Online (Sandbox Code Playgroud)
[Serializable]
private sealed class NullStream : Stream {
internal NullStream() { }
public override bool CanRead {
get { return true; }
}
public override bool CanWrite {
get { return true; }
}
public override bool CanSeek {
get { return true; }
}
public override long Length {
get { return 0; }
}
public override long Position {
get { return 0; }
set { }
}
// No need to override Close
public override void Flush() {
}
public override int Read([In, Out] byte[] buffer, int offset, int count) {
return 0;
}
public override int ReadByte() {
return -1;
}
public override void Write(byte[] buffer, int offset, int count) {
}
public override void WriteByte(byte value) {
}
public override long Seek(long offset, SeekOrigin origin) {
return 0;
}
public override void SetLength(long length) {
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
对我来说最好的解决方案是将 Console.WriteLine() 更改为 System.Diagnostics.Debug.WriteLine()。例如:
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
System.Diagnostics.Debug.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
Run Code Online (Sandbox Code Playgroud)
然后您可以在输出窗口中将错误作为对象查看。
| 归档时间: |
|
| 查看次数: |
32976 次 |
| 最近记录: |