{"id":5269,"date":"2014-03-30T20:07:11","date_gmt":"2014-03-30T20:07:11","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/console-error-and-console-out-not-writing-on-redirected-file-streams-collection-of-common-programming-errors\/"},"modified":"2014-03-30T20:07:11","modified_gmt":"2014-03-30T20:07:11","slug":"console-error-and-console-out-not-writing-on-redirected-file-streams-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/console-error-and-console-out-not-writing-on-redirected-file-streams-collection-of-common-programming-errors\/","title":{"rendered":"Console.Error and Console.Out not writing on Redirected File Streams-Collection of common programming errors"},"content":{"rendered":"<pre><code>namespace Pro\n{\n    class ErrorLog\n    {\n        public ErrorLog(RenderWindow app)\n        {\n            startTime = DateTime.Now.ToString(\"yyyyMMddHHmm\");\n            outFile = @\"data\\runtime\\\" + startTime + \".log\";\n            errFile = @\"data\\runtime\\\" + startTime + \".err\";\n\n            try\n            {\n                OutputStream = new StreamWriter(outFile);\n                ErrorStream = new StreamWriter(errFile);\n            }\n            catch (Exception e)\n            {\n                Console.Error.WriteLine(e);\n                MessageBox.Show(e.Message);\n                Environment.Exit(1);\n            }\n\n            var info = new ComputerInfo();\n\n            Console.SetOut(OutputStream);\n            Console.SetError(ErrorStream);\n            Console.WriteLine(\"Start Time: {0}\", startTime);\n            Console.WriteLine(\"Platform: {0}\", info.OSFullName);            \n            Console.WriteLine(\"Available Memory: {0}\", info.AvailablePhysicalMemory);\n\n            ReportApplicationData(app);\n\n        }\n\n        ~ErrorLog()\n        {\n            if (wereErrors)\n            {\n                var msg = string.Format(\"There were some runtime errors. Kindly see {0} for the error messages and {1} for the runtime log\",\n                    errFile, outFile);\n                DisplayMessage(msg);                \n            }\n\n            Console.Error.Close();\n            Console.Out.Close();\n        }\n\n        public void ReportApplicationData(RenderWindow app)\n        {          \n            this.app = app;\n\n            Console.WriteLine(\"Screen Width = {0}, Height = {1}, Depth = {2}\",\n                app.Width, app.Height, app.Settings.DepthBits);\n        }\n\n        public void DisplayMessage(string msg)\n        {\n            Console.WriteLine(\"Application Message : '{0}'\", msg);\n            MessageBox.Show(msg);\n        }\n\n        public void DisplayError(bool fatal, string format, params object[] arg)\n        {\n            Console.Error.WriteLine(format, arg);\n            wereErrors = true;\n\n            if (fatal) app.Close();\n        }\n\n        public TextWriter ErrorStream\n        {\n            get;\n            private set;\n        }\n\n        public TextWriter OutputStream\n        {\n            get;\n            set;\n        }\n\n        bool wereErrors = false;\n\n        string startTime;\n        string errFile;\n        string outFile;\n\n        RenderWindow app; \/\/ SFML RenderWindow\n    }\n}\n<\/code><\/pre>\n<p>I create an instance of <code>ErrorLog<\/code> in the <code>Main<\/code> method. But, for some reason nothing gets written to the <code>.log<\/code> and <code>.err<\/code> files even though they are created. All I see is two empty files with a size of 0 bytes, with no text written in them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>namespace Pro { class ErrorLog { public ErrorLog(RenderWindow app) { startTime = DateTime.Now.ToString(&#8220;yyyyMMddHHmm&#8221;); outFile = @&#8221;data\\runtime\\&#8221; + startTime + &#8220;.log&#8221;; errFile = @&#8221;data\\runtime\\&#8221; + startTime + &#8220;.err&#8221;; 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(&#8220;Start Time: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5269","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5269"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5269\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}