Constant file size

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adrián E. Córdoba

    Constant file size

    Hi, there!
    I'm developing a little application which must record some events in a
    log file (.txt).
    How can I delete the first line in the log file every time I add a new
    line at the end, in order to keep constant the file size?

    Thank you in advance.
    --
    Adrián E. Córdoba

  • Andy

    #2
    Re: Constant file size

    If you need logging, I suggest looking at the log4net project, found at
    the Apache Foundations site. They have a LogFileAppender , which has
    the functionality you describe.

    HTH
    Andy


    Adrián E. Córdoba wrote:
    Hi, there!
    I'm developing a little application which must record some events in a
    log file (.txt).
    How can I delete the first line in the log file every time I add a new
    line at the end, in order to keep constant the file size?

    Thank you in advance.
    --
    Adrián E. Córdoba

    Comment

    • Adrián E. Córdoba

      #3
      Re: Constant file size

      Sorry, Andy. But I think LogFileAppender always add new lines to the
      log file, so the file increase its size.
      I need to keep constant the size of the log file.

      Thank you, for your comments.
      --
      Adrián

      Andy ha escrito:
      If you need logging, I suggest looking at the log4net project, found at
      the Apache Foundations site. They have a LogFileAppender , which has
      the functionality you describe.
      >
      HTH
      Andy
      >
      >
      Adrián E. Córdoba wrote:
      Hi, there!
      I'm developing a little application which must record some events in a
      log file (.txt).
      How can I delete the first line in the log file every time I add a new
      line at the end, in order to keep constant the file size?

      Thank you in advance.
      --
      Adrián E. Córdoba

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Constant file size

        Adrián E. Córdoba <software.asia@ gmail.comwrote:
        I'm developing a little application which must record some events in a
        log file (.txt).
        How can I delete the first line in the log file every time I add a new
        line at the end, in order to keep constant the file size?
        Two things:

        1) Keeping the same number of lines *won't* keep the file the same
        size, unless all lines are the same size

        2) Deleting the first line in a file involves rewriting the whole file.
        If you use a fixed-size encoding (eg Encoding.Unicod e) and keep the
        lines the same size, you can make it a rolling log file, writing into
        the middle of the file, overwriting lines on a rolling basis.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Adrián E. Córdoba

          #5
          Re: Constant file size

          Thank you for your comments, Jon.

          1) It is not necessary the file size will be exactly XX MB. It's enough
          to keep constant the number of lines.

          2) Working with a "rolling log file" and writting in the middle of the
          file, make the file hard to read if you open the file with simple text
          applications like Notepad because you need to find the oldest line in
          the file. It is possible but it isn't desirable.

          Thank you, again.

          --
          Adrián


          Jon ha escrito:
          Adrián E. Córdoba <software.asia@ gmail.comwrote:
          I'm developing a little application which must record some events in a
          log file (.txt).
          How can I delete the first line in the log file every time I add a new
          line at the end, in order to keep constant the file size?
          >
          Two things:
          >
          1) Keeping the same number of lines *won't* keep the file the same
          size, unless all lines are the same size
          >
          2) Deleting the first line in a file involves rewriting the whole file.
          If you use a fixed-size encoding (eg Encoding.Unicod e) and keep the
          lines the same size, you can make it a rolling log file, writing into
          the middle of the file, overwriting lines on a rolling basis.
          >
          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Constant file size

            Adrián E. Córdoba wrote:
            Thank you for your comments, Jon.
            >
            1) It is not necessary the file size will be exactly XX MB. It's enough
            to keep constant the number of lines.
            Okay.
            2) Working with a "rolling log file" and writting in the middle of the
            file, make the file hard to read if you open the file with simple text
            applications like Notepad because you need to find the oldest line in
            the file. It is possible but it isn't desirable.
            In that case, unless you *really* want to rewrite the whole log file
            each time, I suggest you roll over to different log files (deleting old
            ones) rather than try to keep it all in one log.

            Jon

            Comment

            • Andy

              #7
              Re: Constant file size

              Just to add to what Jon said, if you do go the rolling log method,
              log4net does support that natively. No need to write your own.

              Andy

              Jon Skeet [C# MVP] wrote:
              Adrián E. Córdoba wrote:
              Thank you for your comments, Jon.

              1) It is not necessary the file size will be exactly XX MB. It's enough
              to keep constant the number of lines.
              >
              Okay.
              >
              2) Working with a "rolling log file" and writting in the middle of the
              file, make the file hard to read if you open the file with simple text
              applications like Notepad because you need to find the oldest line in
              the file. It is possible but it isn't desirable.
              >
              In that case, unless you *really* want to rewrite the whole log file
              each time, I suggest you roll over to different log files (deleting old
              ones) rather than try to keep it all in one log.

              Jon

              Comment

              • Adrián E. Córdoba

                #8
                Re: Constant file size

                Thanks to all.

                --
                Adrián

                Andy ha escrito:
                Just to add to what Jon said, if you do go the rolling log method,
                log4net does support that natively. No need to write your own.
                >
                Andy
                >
                Jon Skeet [C# MVP] wrote:
                Adrián E. Córdoba wrote:
                Thank you for your comments, Jon.
                >
                1) It is not necessary the file size will be exactly XX MB. It's enough
                to keep constant the number of lines.
                Okay.
                2) Working with a "rolling log file" and writting in the middle of the
                file, make the file hard to read if you open the file with simple text
                applications like Notepad because you need to find the oldest line in
                the file. It is possible but it isn't desirable.
                In that case, unless you *really* want to rewrite the whole log file
                each time, I suggest you roll over to different log files (deleting old
                ones) rather than try to keep it all in one log.

                Jon

                Comment

                Working...