gpt4 book ai didi

c# - Console.Error 和 Console.Out 不写入重定向文件流

转载 作者:行者123 更新时间:2023-11-30 15:03:14 35 4
gpt4 key购买 nike

namespace Pro
{
class ErrorLog
{
public ErrorLog(RenderWindow app)
{
startTime = DateTime.Now.ToString("yyyyMMddHHmm");
outFile = @"data\runtime\" + startTime + ".log";
errFile = @"data\runtime\" + startTime + ".err";

try
{
OutputStream = new StreamWriter(outFile);
ErrorStream = new StreamWriter(errFile);
}
catch (Exception e)
{
Console.Error.WriteLine(e);
MessageBox.Show(e.Message);
Environment.Exit(1);
}

var info = new ComputerInfo();

Console.SetOut(OutputStream);
Console.SetError(ErrorStream);
Console.WriteLine("Start Time: {0}", startTime);
Console.WriteLine("Platform: {0}", info.OSFullName);
Console.WriteLine("Available Memory: {0}", info.AvailablePhysicalMemory);

ReportApplicationData(app);

}

~ErrorLog()
{
if (wereErrors)
{
var msg = string.Format("There were some runtime errors. Kindly see {0} for the error messages and {1} for the runtime log",
errFile, outFile);
DisplayMessage(msg);
}

Console.Error.Close();
Console.Out.Close();
}

public void ReportApplicationData(RenderWindow app)
{
this.app = app;

Console.WriteLine("Screen Width = {0}, Height = {1}, Depth = {2}",
app.Width, app.Height, app.Settings.DepthBits);
}

public void DisplayMessage(string msg)
{
Console.WriteLine("Application Message : '{0}'", msg);
MessageBox.Show(msg);
}

public void DisplayError(bool fatal, string format, params object[] arg)
{
Console.Error.WriteLine(format, arg);
wereErrors = true;

if (fatal) app.Close();
}

public TextWriter ErrorStream
{
get;
private set;
}

public TextWriter OutputStream
{
get;
set;
}

bool wereErrors = false;

string startTime;
string errFile;
string outFile;

RenderWindow app; // SFML RenderWindow
}
}

我在 Main 方法中创建了一个 ErrorLog 实例。但是,出于某种原因,即使创建了 .log.err 文件,也没有写入任何内容。我只看到两个大小为 0 字节的空文件,其中没有写入任何文本。

最佳答案

你必须刷新你的流,即:

OutputStream.Flush();

关于c# - Console.Error 和 Console.Out 不写入重定向文件流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11808481/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com