[C#] Saving Logs To A TXT File One At A Time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danrabydotcom
    New Member
    • Aug 2008
    • 11

    [C#] Saving Logs To A TXT File One At A Time

    I have an application that reaches out and gathers stats, if those stats are out of bounds, it logs them.

    Currently I have a string builder thats accumulates all logs durring the session and writes them all to the file at the end. This is a memory hog. Is it ok to open a text file every 2-4 seconds for 6 hours.
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    I don't see a problem with that. Although, you don't really have to open and close the file each time. If you aren't going to be reading from it, you can just open it at the beginning. Write each entry in real time, then close the file when the application exits.

    Either way can work.

    Comment

    • danrabydotcom
      New Member
      • Aug 2008
      • 11

      #3
      Originally posted by insertAlias
      I don't see a problem with that. Although, you don't really have to open and close the file each time. If you aren't going to be reading from it, you can just open it at the beginning. Write each entry in real time, then close the file when the application exits.

      Either way can work.
      What method would you use to write to the file. I'm new to C# and I can't make it through my books fast enough to develop what I need. lol. Stupid 1600 page books.

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        As long as nothing else needs to access it, I'd leave it open. But I'd make sure to catch any exceptions in the program at all, and in the finally statement, close the stream.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Originally posted by insertAlias
          I don't see a problem with that. Although, you don't really have to open and close the file each time. If you aren't going to be reading from it, you can just open it at the beginning. Write each entry in real time, then close the file when the application exits.

          Either way can work.
          Sounds great in theory but doesn't this pose a problem in the real world where power goes out, computers drop suddenly, networks go down while the destination file is on a server someplace else?

          What happens to the file while it is open for 6 hours, when the power goes out and it is not closed properly?

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Yeah, didn't think about that. Of course, it's still possible for that to happen if the file is being open and closed every few seconds, but it does reduce the risk somewhat.

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              After re-reading your question I had one other 'real world' consideration and option... <every 2-4 *seconds* for 6 hours a day> That really is just hammering the hard drive.

              There are some good logging classes already out there
              [HTML]http://www.codeproject .com/KB/cs/ObserverLogging .aspx?fid=85410 6&df=90&mpp=25& noise=3&sort=Po sition&view=Qui ck&fr=26#xx0xx[/HTML]
              as well as the built in Trace function that have buffering built in. So you write several lines to your logger object and it remains in memory until you flush it, causing it to actually write to the drive. You could add a timer for 30-60 seconds and thus only write 1-2 times a minute instead of 20-30 times a minute, reducing drive wear and tear.

              Pro: You aren't hammering the drive every 2 seconds.
              Con: You could loose the last 20 seconds of log events if the power goes out. (but that' what smart UPS is for with USB connect so you get an event for the power out and can clean up nicely then shut down)

              Comment

              • danrabydotcom
                New Member
                • Aug 2008
                • 11

                #8
                Our whole infrastructure is on UPS and generator. And the information isn't LifeSaving. So losing a few seconds of info won't hurt. I just want to make sure that in my newbie state, i don't implement something that will be processor/computer heavy. I am going to rewrite the whole project as its own self containted namespace so i can add it to another global app already in production.

                Once I get that done I will make the changes to the saving times.

                Thanks Everyone For Your Help!

                Comment

                Working...