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