Trimming the appended FileStream

M

Matijaz

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;
}
}
 
S

Sergey Bogdanov

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...
 
M

Matijaz

Thanks Siergiej, your solution works very well!

I have some experience using log4j in my Java projects. But now I'm working
on some commercial project in .NET and I was affraid to use log4net as soon
as I saw this is beta release. Is it stable enough?

Regards
Matijaz
 
S

Sergey Bogdanov

It is enough stable and to tell the truth, I even did't have any
problems with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top