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. Limiting the size of log file

Limiting the size of log file

Scheduled Pinned Locked Moved C#
question
7 Posts 6 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.
  • E Offline
    E Offline
    eyalbi007
    wrote on last edited by
    #1

    Hi Guys, I'm redirecting the output of an external process to a function which writes the data to a log file using StreamWriter.Write() method. Is there a smart way of limiting that file size? Thanks! Eyal.

    G D 2 Replies Last reply
    0
    • E eyalbi007

      Hi Guys, I'm redirecting the output of an external process to a function which writes the data to a log file using StreamWriter.Write() method. Is there a smart way of limiting that file size? Thanks! Eyal.

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      Try log4net. It can automatically create new log file when an old one exceeds the size you specify.

      Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

      realJSOPR B 2 Replies Last reply
      0
      • E eyalbi007

        Hi Guys, I'm redirecting the output of an external process to a function which writes the data to a log file using StreamWriter.Write() method. Is there a smart way of limiting that file size? Thanks! Eyal.

        D Offline
        D Offline
        Dragonfly_Lee
        wrote on last edited by
        #3

        We can get the size of file through FileInfo. Hence, we can decide whether to create a new log if the size is out of limitation. Such as FileInfo fileInfo = new FileInfo("1.rar"); if(fileInfo.Length > 100*1024) { //create a new log file }

        Tan Li I Love KongFu~

        L 1 Reply Last reply
        0
        • D Dragonfly_Lee

          We can get the size of file through FileInfo. Hence, we can decide whether to create a new log if the size is out of limitation. Such as FileInfo fileInfo = new FileInfo("1.rar"); if(fileInfo.Length > 100*1024) { //create a new log file }

          Tan Li I Love KongFu~

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Dragonfly_Lee wrote:

          to create a new log if the size is out of limitation

          Instead of deleting the old log, start overwriting; you'll loose information if you just wipe the logfile. Alternatively, you could delete the oldest entry every time you add a new one. Whether this is feasible depends on the maximum size of the logfile.

          I are troll :)

          D 1 Reply Last reply
          0
          • G Giorgi Dalakishvili

            Try log4net. It can automatically create new log file when an old one exceeds the size you specify.

            Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            I'm assuming that the OP wants a log file that will never get larger than size N, and that older entries are deleted as necessary. Creating a new file seems to me to be a sloppy workaround. I can't think of any way to do this other than to load the entire file into memory, delete what makes it too large, and then save it again.

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            1 Reply Last reply
            0
            • G Giorgi Dalakishvili

              Try log4net. It can automatically create new log file when an old one exceeds the size you specify.

              Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

              B Offline
              B Offline
              blackjack2150
              wrote on last edited by
              #6

              I second. Look for RollingFileAppender class and the RollingStyle property. My suggestion is to create separate logs for each day/week/hour/whatever and to delete the logs older than the x-th version.

              modified on Wednesday, January 7, 2009 11:37 AM

              1 Reply Last reply
              0
              • L Lost User

                Dragonfly_Lee wrote:

                to create a new log if the size is out of limitation

                Instead of deleting the old log, start overwriting; you'll loose information if you just wipe the logfile. Alternatively, you could delete the oldest entry every time you add a new one. Whether this is feasible depends on the maximum size of the logfile.

                I are troll :)

                D Offline
                D Offline
                Dragonfly_Lee
                wrote on last edited by
                #7

                Eddy Vluggen wrote:

                Instead of deleting the old log, start overwriting

                I did not mean to delete the log file. We can create a new log file, such as: 1.log 2.log ... So we will not lose any information. However, if the log file size is too huge, we also need to consider the disk space.

                I Love KongFu~

                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