Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9306395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:09:50+00:00 2026-06-19T00:09:50+00:00

An external Windows service I work with maintains a single text-based log file that

  • 0

An external Windows service I work with maintains a single text-based log file that it continuously appends to. This log file grows unbounded over time. I’d like to prune this log file periodically to maintain, say the most recent 5mb of log entries. How can I efficiently implement the file I/O code in C# .NET 4.0 to prune the file to say 5mb?

Updated:
The way service dependencies are set up, my service always starts before the external service. This means I get exclusive access to the log file to truncate it, if required. Once the external service starts up, I will not access the log file. I can gain exclusive access to the file on desktop startup. The problem is – the log file may a few gigabytes in size and I’m looking for an efficient way to truncate it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:09:52+00:00Added an answer on June 19, 2026 at 12:09 am

    It’s going to take the amount of memory that you want to store to process the “new” log file but if you only want 5Mb then it should be fine. If you are talking about Gb+ then you probably have other problems; however, it could still be accomplished using a temp file and some locking.

    As noted before, you may experience a race condition but that’s not the case if this is the only thread writing to this file. This would replace your current writing to the file.

    const int MAX_FILE_SIZE_IN_BYTES = 5 * 1024 * 1024; //5Mb;
    const string LOG_FILE_PATH = @"ThisFolder\log.txt";
    string newLogMessage = "Hey this happened";
    
    
    #region Use one or the other, I mean you could use both below if you really want to.
    //Use this one to save an extra character
    if (!newLogMessage.StartsWith(Environment.NewLine))
        newLogMessage = Environment.NewLine + newLogMessage;
    
    //Use this one to imitate a write line
    if (!newLogMessage.EndsWith(Environment.NewLine))
        newLogMessage = newLogMessage + Environment.NewLine; 
    #endregion
    
    int newMessageSize = newLogMessage.Length*sizeof (char);
    byte[] logMessage = new byte[MAX_FILE_SIZE_IN_BYTES];
    //Append new log to end of "file"
    System.Buffer.BlockCopy(newLogMessage.ToCharArray(), 0, logMessage, MAX_FILE_SIZE_IN_BYTES - newMessageSize, logMessage.Length);
    
    
    FileStream logFile = File.Open(LOG_FILE_PATH, FileMode.Open, FileAccess.ReadWrite);
    
    int sizeOfRetainedLog = (int)Math.Min(MAX_FILE_SIZE_IN_BYTES - newMessageSize, logFile.Length);
    //Set start position/offset of the file
    logFile.Position = logFile.Length - sizeOfRetainedLog;
    //Read remaining portion of file to beginning of buffer
    logFile.Read(logMessage, logMessage.Length, sizeOfRetainedLog);
    
    //Clear the file
    logFile.SetLength(0); 
    logFile.Flush();
    
    //Write the file
    logFile.Write(logMessage, 0, logMessage.Length);
    

    I wrote this really quick, I apologize if I’m off by 1 somewhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Windows Service written in C# that handles all of our external
I have a windows service which reads the config settings from an external file
I have the following situation: I have a windows service that loads many external
I have written a Windows service that spawns a separate process. This process creates
I have a program I'm converting to a Windows Service (in C#). This program
We have an external service that is currently accessible via the http (port 80,
We have this strange bug report from a customer: Log file last modification date
I am starting software service as external .exe and than as windows services, but
I have a Windows service which I want to periodically execute an external program.
I have a Windows Service which receives data (every seconds) from an external componant.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.