相关疑难解决方法(0)

如何将Console.WriteLine输出保存到文本文件

我有一个程序,可以将各种结果输出到命令行控制台.

如何使用StreamReader其他技术将输出保存到文本文件?

System.Collections.Generic.IEnumerable<String> lines = File.ReadAllLines(@"C:\Test\ntfs8.txt");

foreach (String r in lines.Skip(1))
{
    String[] token = r.Split(',');
    String[] datetime = token[0].Split(' ');
    String timeText = datetime[4];
    String actions = token[2];
    Console.WriteLine("The time for this array is: " + timeText);
    Console.WriteLine(token[7]);
    Console.WriteLine(actions);
    MacActions(actions);
    x = 1;
    Console.WriteLine("================================================");
}

if (x == 2)
{
    Console.WriteLine("The selected time does not exist within the log files!");
}

System.IO.StreamReader reader = ;
string sRes = reader.ReadToEnd();
StreamWriter SW;
SW = File.CreateText("C:\\temp\\test.bodyfile");
SW.WriteLine(sRes);
SW.Close();
Console.WriteLine("File …
Run Code Online (Sandbox Code Playgroud)

c#

87
推荐指数
6
解决办法
17万
查看次数

使用System.Diagnostics.Debugger.Log打印换行符

我在我的日志中打印了很多行,同时调试如下:

System.Diagnostics.Debugger.Log(0, null, responseFromServer);
System.Diagnostics.Debugger.Log(0, null, t[0]); 
....
Run Code Online (Sandbox Code Playgroud)

所有这些都在同一条线上印刷.我怎样才能让它们以单独的线条印刷?

我试过用

System.Diagnostics.Debugger.Log(0, null, t[0]+"\n");
Run Code Online (Sandbox Code Playgroud)

但是,它没有用.任何帮助将不胜感激 .谢谢

c# system.diagnostics

6
推荐指数
1
解决办法
7620
查看次数

使用跟踪侦听器将调试输出重定向到文件时出现问题

我创建了一个调试监听器,使用以下代码将Debug/Console窗口的输出重定向到一个文件(带有一个调用堆栈):

void SomeMethod()
{
    // Create a file for output .txt.
    Stream debugFile = File.Create(fileName);

    // create TextWriterTraceListener named "file"
    TextWriterTraceListener debugWriter = new TextWriterTraceListener(debugFile, "file");

    // add to debug listeners
    Debug.Listeners.Add(debugWriter);
    // set callstack to be shown
    Debug.Listeners["file"].TraceOutputOptions |= TraceOptions.Callstack;
    // set auto-flush
    Debug.AutoFlush = true;
}
Run Code Online (Sandbox Code Playgroud)

但输出不会重定向到我指定的文件,它总是为空.

我从我的主窗体中的构造函数中调用它.我是从问题中调用它的地方吗?

我在这里尝试实现的是将调试输出窗口中的异常放在带有调用堆栈的文件中,以便我可以找到它们并更正它们.

更新:经过一些研究后,我得出结论,TraceListener在Debug Listeners集合中添加一个新的不会重定向调试/控制台的输出.它实际上只是回应Write,WriteLine等方法一样默认的监听器.问题仍然存在:如何捕获Debug/Console窗口的输出以及如何获取出现在那里的异常的堆栈跟踪?

有人有主意吗?

.net c# debugging redirect tracelistener

5
推荐指数
1
解决办法
5308
查看次数