public void WriteLog(string context)
{
if (!string.IsNullOrEmpty(logFilePath))
{
FileStream fileStream = null;
if (File.Exists(logFilePath))
{
fileStream = new FileStream(logFilePath, FileMode.Append, FileAccess.Write);
}
else
{
Directory.CreateDirectory(logFilePath.Substring(0, logFilePath.LastIndexOf("\\")));
fileStream = new FileStream(logFilePath, FileMode.Create, FileAccess.Write);
}
StreamWriter writer = new StreamWriter(fileStream);
try
{
FileInfo fi = new FileInfo(logFilePath);
string fileName;
if (fi.Length >= 1024 * 1024 * 5)
{
fileName = fi.FullName.Substring(0, fi.FullName.Length - 4) + "." + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(logFilePath);
writer.Close();
if (!File.Exists(fileName))
{
File.Move(logFilePath, fileName);
writer = File.CreateText(logFilePath);
}
else
writer = File.AppendText(logFilePath);
}
writer.WriteLine(string.Empty.PadLeft(20, '=') + DateTime.Now.ToString() + string.Empty.PadLeft(20, '='));
writer.WriteLine(context);
writer.Flush();
writer.Close();
}
finally
{
fileStream.Close();
writer.Close();
}
}
}