Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Console.Error和Console.Out未写入重定向的文件流_C#_.net_Console_Stdout_Stderr - Fatal编程技术网

C# Console.Error和Console.Out未写入重定向的文件流

C# Console.Error和Console.Out未写入重定向的文件流,c#,.net,console,stdout,stderr,C#,.net,Console,Stdout,Stderr,我在Main方法中创建了ErrorLog的一个实例。但是,由于某些原因,即使创建了.log和.err文件,也不会向它们写入任何内容。我只看到两个大小为0字节的空文件,其中没有写入任何文本。您必须刷新流,即: namespace Pro { class ErrorLog { public ErrorLog(RenderWindow app) { startTime = DateTime.Now.ToString("yyyyMM

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

您必须刷新流,即:

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
    }
}

我应该在析构函数中这样做吗?首先,使用正在使用的(outputstream=StreamWriter(outFile))而不是析构函数是一种很好的做法
OutputStream.Flush();