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