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. Limit size of text file (LIFO)

Limit size of text file (LIFO)

Scheduled Pinned Locked Moved C#
csharpquestion
12 Posts 7 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.
  • M Offline
    M Offline
    Maddie from Dartford
    wrote on last edited by
    #1

    Hello, I have implemented a logger which is writing the data to a text file. But, the file size gets huge if all the data is written to it. I want to keep only last one hour data in file. The logger should keep writing all data till one hour and after that it should follow LIFO structure. So that the file size will not be uncontrolled. How it can be done in C#? Thanks a lot.

    M N K V B 5 Replies Last reply
    0
    • M Maddie from Dartford

      Hello, I have implemented a logger which is writing the data to a text file. But, the file size gets huge if all the data is written to it. I want to keep only last one hour data in file. The logger should keep writing all data till one hour and after that it should follow LIFO structure. So that the file size will not be uncontrolled. How it can be done in C#? Thanks a lot.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      LIFO is going to be a PITA - why not split the log files into hourly files, just keep withing to the file name, on the hour create a new file and then your garbage collection is a simple jod to delete the old files. Saves thrashing around inside the text file.

      M 1 Reply Last reply
      0
      • M Mycroft Holmes

        LIFO is going to be a PITA - why not split the log files into hourly files, just keep withing to the file name, on the hour create a new file and then your garbage collection is a simple jod to delete the old files. Saves thrashing around inside the text file.

        M Offline
        M Offline
        Maddie from Dartford
        wrote on last edited by
        #3

        Thanks Holmes, Actually, the data is getting logged from two different events to same file and i am just writing over there. At a given condition only one event will be fired. The file creation is done in constructor and the same name is referred to write into it. If we can limit either text length or size then it would be easier.

        M 1 Reply Last reply
        0
        • M Maddie from Dartford

          Thanks Holmes, Actually, the data is getting logged from two different events to same file and i am just writing over there. At a given condition only one event will be fired. The file creation is done in constructor and the same name is referred to write into it. If we can limit either text length or size then it would be easier.

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          gajesh wrote:

          two different events

          That makes your exposure worse, it will take an appreciable amount of time to extract the first in lines from a text file, read in whole file, remove the offending lines and write out the entire file each time you write an entry. You are open to locking if the events can fire too close in time. I don't know of any way to remove the first in lines elegantly.

          1 Reply Last reply
          0
          • M Maddie from Dartford

            Hello, I have implemented a logger which is writing the data to a text file. But, the file size gets huge if all the data is written to it. I want to keep only last one hour data in file. The logger should keep writing all data till one hour and after that it should follow LIFO structure. So that the file size will not be uncontrolled. How it can be done in C#? Thanks a lot.

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            I'd suggest to try logging frameworks like log4net. It will work much better and it has this feature built in.

            Navaneeth How to use google | Ask smart questions

            M 1 Reply Last reply
            0
            • N N a v a n e e t h

              I'd suggest to try logging frameworks like log4net. It will work much better and it has this feature built in.

              Navaneeth How to use google | Ask smart questions

              M Offline
              M Offline
              Maddie from Dartford
              wrote on last edited by
              #6

              Thanks Navneeth, The download seems to be very big (200+ MB)for log4net. I'll give a try to similar type of logger. I'll also explore if i can create another file as suggested earlier.

              S N 2 Replies Last reply
              0
              • M Maddie from Dartford

                Hello, I have implemented a logger which is writing the data to a text file. But, the file size gets huge if all the data is written to it. I want to keep only last one hour data in file. The logger should keep writing all data till one hour and after that it should follow LIFO structure. So that the file size will not be uncontrolled. How it can be done in C#? Thanks a lot.

                K Offline
                K Offline
                kb boxer
                wrote on last edited by
                #7

                I guess you have to resort to using memory mapped files. And when an hour passes, you would have to move up the records except the first one (LIFO). Hope that helps.

                http://vivekragunathan.spaces.live.com

                Programming is an art. Code is a poem

                1 Reply Last reply
                0
                • M Maddie from Dartford

                  Hello, I have implemented a logger which is writing the data to a text file. But, the file size gets huge if all the data is written to it. I want to keep only last one hour data in file. The logger should keep writing all data till one hour and after that it should follow LIFO structure. So that the file size will not be uncontrolled. How it can be done in C#? Thanks a lot.

                  V Offline
                  V Offline
                  Vikram A Punathambekar
                  wrote on last edited by
                  #8

                  I second log4net. Anything else is just asking for pain.

                  Cheers, Vikram. (Proud to have finally cracked a CCC!)

                  1 Reply Last reply
                  0
                  • M Maddie from Dartford

                    Thanks Navneeth, The download seems to be very big (200+ MB)for log4net. I'll give a try to similar type of logger. I'll also explore if i can create another file as suggested earlier.

                    S Offline
                    S Offline
                    Saksida Bojan
                    wrote on last edited by
                    #9

                    gajesh wrote:

                    The download seems to be very big (200+ MB)for log4net

                    From where did you download? i only got 7.8MB for it

                    1 Reply Last reply
                    0
                    • M Maddie from Dartford

                      Thanks Navneeth, The download seems to be very big (200+ MB)for log4net. I'll give a try to similar type of logger. I'll also explore if i can create another file as suggested earlier.

                      N Offline
                      N Offline
                      N a v a n e e t h
                      wrote on last edited by
                      #10

                      gajesh wrote:

                      The download seems to be very big (200+ MB)for log4net.

                      No. Its just 7.8MB. This[^] will be the download link. The log4net dll is just 224KB.

                      Navaneeth How to use google | Ask smart questions

                      1 Reply Last reply
                      0
                      • M Maddie from Dartford

                        Hello, I have implemented a logger which is writing the data to a text file. But, the file size gets huge if all the data is written to it. I want to keep only last one hour data in file. The logger should keep writing all data till one hour and after that it should follow LIFO structure. So that the file size will not be uncontrolled. How it can be done in C#? Thanks a lot.

                        B Offline
                        B Offline
                        benjymous
                        wrote on last edited by
                        #11

                        In the unix world, there's a tool called logrotate, which basically loops over all your log files, removes the older lines, and backs them up in an archive - something like that would be relatively trivial to write in c#, and would keep the complexity out of your logging code. The only thing you'd have to be careful of, would be file lock clashes when both the logging tool and the rotate tool try to access the log at the same time

                        Help me! I'm turning into a grapefruit! Buzzwords!

                        M 1 Reply Last reply
                        0
                        • B benjymous

                          In the unix world, there's a tool called logrotate, which basically loops over all your log files, removes the older lines, and backs them up in an archive - something like that would be relatively trivial to write in c#, and would keep the complexity out of your logging code. The only thing you'd have to be careful of, would be file lock clashes when both the logging tool and the rotate tool try to access the log at the same time

                          Help me! I'm turning into a grapefruit! Buzzwords!

                          M Offline
                          M Offline
                          Maddie from Dartford
                          wrote on last edited by
                          #12

                          Thanks a lot, I managed to toggle between two files as a logger as workaround. Still i am keen to explore the right and best way to implement this. I'll post the solution as soon as i am done.

                          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