Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. mysteries of the disapearing text

mysteries of the disapearing text

Scheduled Pinned Locked Moved C#
visual-studiodebugginghelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    freshthinking
    wrote on last edited by
    #1

    I have written a small debug class so I can echo some statements to the ouput in VS and to a text file, using the StreamWriter class... The problem is, is that the text is never written to the file. public class Debug { private StreamWriter _writer; private string path; public Debug(string path) { this.path = path; if( File.Exists(path) ) _writer = File.AppendText(path) else _writer = File.Create(path); } public void WriteLine(string output) { string s = DateTime.Now + "\t" + output; System.Diagnostics.Debug.WriteLine(s); _writer.WriteLine(s); } } Now this is fairly bog standard code foe writing to a file, and even stranger is if i replace the WriteLine procedure with public void WriteLine(string output) { string s = DateTime.Now + "\t" + output; System.Diagnostics.Debug.WriteLine(s); StreamWriter sw = File.AppendText(this.path); sw.WriteLine(s); sw.Close(); } The text is written to the file fine. Can any shed light on this.... What dumbass thing I am I doing wrong?

    R N 2 Replies Last reply
    0
    • F freshthinking

      I have written a small debug class so I can echo some statements to the ouput in VS and to a text file, using the StreamWriter class... The problem is, is that the text is never written to the file. public class Debug { private StreamWriter _writer; private string path; public Debug(string path) { this.path = path; if( File.Exists(path) ) _writer = File.AppendText(path) else _writer = File.Create(path); } public void WriteLine(string output) { string s = DateTime.Now + "\t" + output; System.Diagnostics.Debug.WriteLine(s); _writer.WriteLine(s); } } Now this is fairly bog standard code foe writing to a file, and even stranger is if i replace the WriteLine procedure with public void WriteLine(string output) { string s = DateTime.Now + "\t" + output; System.Diagnostics.Debug.WriteLine(s); StreamWriter sw = File.AppendText(this.path); sw.WriteLine(s); sw.Close(); } The text is written to the file fine. Can any shed light on this.... What dumbass thing I am I doing wrong?

      R Offline
      R Offline
      Rocky Moore
      wrote on last edited by
      #2

      Could it be because you are not explictly closing the stream in your original and maybe it is not being flushed? Personally, in my logging I do not try keep the file open all the time, I use:

      public void Write(string message, LoggingLevel level)
      {
      // logging level and filename check code omitted

      StreamWriter logFile=null;

      lock(this)
      {
      try
      {
      logFile = new StreamWriter(fileName, true);
      logFile.WriteLine(message);
      logFile.Close();
      }
      catch(Exception)
      {
      if(logFile!=null)
      logFile.Close();
      }
      }

      if(isSystemDebugEnabled) // local class variable
      System.Diagnostics.Debug.WriteLine(s);
      }

      Rocky Moore <><

      1 Reply Last reply
      0
      • F freshthinking

        I have written a small debug class so I can echo some statements to the ouput in VS and to a text file, using the StreamWriter class... The problem is, is that the text is never written to the file. public class Debug { private StreamWriter _writer; private string path; public Debug(string path) { this.path = path; if( File.Exists(path) ) _writer = File.AppendText(path) else _writer = File.Create(path); } public void WriteLine(string output) { string s = DateTime.Now + "\t" + output; System.Diagnostics.Debug.WriteLine(s); _writer.WriteLine(s); } } Now this is fairly bog standard code foe writing to a file, and even stranger is if i replace the WriteLine procedure with public void WriteLine(string output) { string s = DateTime.Now + "\t" + output; System.Diagnostics.Debug.WriteLine(s); StreamWriter sw = File.AppendText(this.path); sw.WriteLine(s); sw.Close(); } The text is written to the file fine. Can any shed light on this.... What dumbass thing I am I doing wrong?

        N Offline
        N Offline
        Nnamdi Onyeyiri
        wrote on last edited by
        #3

        you have to fluch the stream. Another Post by NnamdiOnyeyiri l Website

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups