繁体   English   中英

Console.Error和Console.Out不写在重定向文件流上

[英]Console.Error and Console.Out not writing on Redirected File Streams

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文件也没有写入.log文件。 我只看到两个空文件,大小为0字节,没有写入文本。

你必须刷新你的流:

OutputStream.Flush();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM