c# - How to trim a text log files?

Status
Not open for further replies.

office politics

It's all just 1s and 0s
Messages
6,555
Location
in the lab
this function is meant to accept a string holding the file path and resave only the last 1/2. This way log files are limited in size. Is there a better way to do this?

i'm thinking there's a problem where i'm not deleting the old data out of the file.

Code:
        private static void chklog(string logf)
        {
            
            FileStream fs = File.OpenWrite(logf);


            if (fs.Length > 2000000)
            {

                fs.Close();

                string[] nw = File.ReadAllLines(logf);

                FileStream fs2 = File.OpenWrite(logf);

                for (int i = (nw.GetUpperBound(0) - 1) / 2; i < nw.GetUpperBound(0); i++)
                {
                    
                    Byte[] info =
                        new UTF8Encoding(true).GetBytes(nw[i]);

                    // Add some information to the file.
                    fs2.Write(info, 0, info.Length);



                }
            
            }
           

                fs.Close();
            




        }
 
Instead of this, have you looked into using a logging library like log4net. This will allow you to configure your log files to automatically roll over.

As for the code. It looks like you're closing fs twice and not closing fs2.
 
well originally, i thought that the file needs to be closed and re opened. now that i look at this again, i open the file and read a property. So you're right i prolly dont need that first close. I can just start reading the file.

im guessing a try catch would also allow for custom error checking. I'd rather just allow the standard errors to appear, at least for now. Is there anything i need to add to the catch for this to happen? standard errors being anything .net would add to the console window.

i may just look into the log4net library though. i dont think i ever used someone eles's library though. hmm, do i need to add a using statement and store the files in a particular folder?
 
I was talking in regards to the code he had there, i.e if that's how he was gonna do it. But you're right, first fs close isn't needed, and fs2 is redundant.

I'd much prefer to write this in c++ :p haven't done c# for a good while. That and I don't have a c# compiler installed
 
well originally, i thought that the file needs to be closed and re opened. now that i look at this again, i open the file and read a property. So you're right i prolly dont need that first close. I can just start reading the file.

im guessing a try catch would also allow for custom error checking. I'd rather just allow the standard errors to appear, at least for now. Is there anything i need to add to the catch for this to happen? standard errors being anything .net would add to the console window.

i may just look into the log4net library though. i dont think i ever used someone eles's library though. hmm, do i need to add a using statement and store the files in a particular folder?

I'm getting a little out of my depth because I can't take differences beteween java and c# into account. In java you can use a try ... finally without a catch and throw the exception from the method.
Code:
public void method() throws IOException {
FileStreams input, output
try {
// your file reading / writing stuff
} finally {
input.close
output.close
}
}
I'd much prefer to write this in c++ :p
Bleh. Each to his own eh.
 
Status
Not open for further replies.
Back
Top Bottom