Re: Trimming the appended FileStream
- From: Sergey Bogdanov <sergey.bogdanov@xxxxxxxxx>
- Date: Sun, 10 Jul 2005 00:30:52 +0300
Just open a file in OpenOrCreate mode and then use FileStream.Seek(0, SeekOrigin.End) to append new text to the end of log file. If you want to trim the log file, use for that FileStream.SetLength(TrimToSize) method.
PS:
In all my projects I'm using log4net which has lots of functionality: different appenders, multithreading, output formatting, filters. It's not necessary to invent the wheel each time...
-- Sergey Bogdanov [.NET CF MVP, MCSD] http://www.sergeybogdanov.com
Matijaz wrote:
Helo, I would like to write very simple logger with cleaning capability if the log file exceeds specified size. But 'TruncateLogFile()' function works only if ctor parameter 'fs' is provided as 'new System.IO.FileStream(FILES_PATH + logFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)'. If set as append mode like this new System.IO.FileStream(FILES_PATH + logFileName, IO.FileMode.Append, IO.FileAccess.Write) it throws IOException instead of trimming the log file. How to open FileStream to append with file seek option?
Here comes the code:
public class Log { private const string LOGGER = "LOGGER> "; private const long MAX_FILE_SIZE = 524288; private FileStream m_FileStream; private StreamWriter logFile; private bool isWriteCalled; private bool consoleOutput;
// 2005.07.07 FileStream added to truncate log if > MAX_FILE_SIZE public Log(FileStream fs, bool console) { m_FileStream = fs; logFile = new StreamWriter(fs); consoleOutput = console; this.TruncateLogFile(); this.AddHeader();
}
protected void AddHeader() { string startSession = "Log session started: " + DateTime.Now.ToLongDateString() + " at " + DateTime.Now.ToLongTimeString(); logFile.Write("\r\n\r\n\r\n\r\n"); logFile.WriteLine(startSession); if (consoleOutput) Console.WriteLine(LOGGER + startSession); logFile.Write("\r\n\r\n"); }
public bool TruncateLogFile() { try { if ((m_FileStream.Length > MAX_FILE_SIZE)) { //m_FileStream.Flush(); m_FileStream.SetLength(0); return true; } } catch (Exception ex) { Console.WriteLine(">>>"+ex.Message); } return false; }
public void Write(string str) { if (isWriteCalled) { logFile.Write(str); if (consoleOutput) Console.Write(str); } else { logFile.Write(DateTime.Now.ToLongTimeString() + " " + str); if (consoleOutput) Console.Write(str); } isWriteCalled = true; }
public void WriteLine(string line) { if (isWriteCalled) { logFile.WriteLine(line); if (consoleOutput) Console.WriteLine(line); } else { logFile.WriteLine(DateTime.Now.ToLongTimeString() + " " + line); if (consoleOutput) Console.WriteLine(line); } if (this.TruncateLogFile()) this.AddHeader(); isWriteCalled = false; } }
.
- Follow-Ups:
- Re: Trimming the appended FileStream
- From: Matijaz
- Re: Trimming the appended FileStream
- References:
- Trimming the appended FileStream
- From: Matijaz
- Trimming the appended FileStream
- Prev by Date: Re: CeRunAppAtEvent on TimeZone Change
- Next by Date: Re: How to regist a hot key for my program in smartphone with .net cf?
- Previous by thread: Trimming the appended FileStream
- Next by thread: Re: Trimming the appended FileStream
- Index(es):
Relevant Pages
|