Limit size of text file (LIFO)
-
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.
-
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.
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.
-
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.
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.
-
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.
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.
-
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.
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
-
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
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.
-
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.
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
-
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.
I second log4net. Anything else is just asking for pain.
Cheers, Vikram. (Proud to have finally cracked a CCC!)
-
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.
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
-
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.
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
-
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.
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!
-
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!
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.