写入日志文件
namespace HRPOWER.Service
{
public class WriteLog
{
private XmlDocument xmldoc = new XmlDocument();
private string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "\" + "log";
public WriteLog()
{
if (!Directory.Exists(logFilePath))
{
Directory.CreateDirectory(logFilePath);
}
}
public void write(string logTxt)
{
string fileName = logFilePath + "\" + DateTime.Now.ToString("yyyyMM") + "_log.txt";
FileStream fileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
StreamWriter streamWrite = new StreamWriter(fileStream);
streamWrite.WriteLine(DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss : ") + logTxt);
//写入日志内容并换行
streamWrite.Write(input+"
");
streamWrite.Write("------------------------------------------
");
streamWrite.Flush();
streamWrite.Close();
}
//判断文件是否存在及是否大于2K
if(finfo.Length>1024 *1024 *10)
{
//文件超过10MB则 重命名
File.Move(Directory.GetCurrentDirectory()+"\LogFile.txt",Directory.GetCurrentDirectory()+DateTime.Now.TimeOfDay+"\LogFile.txt");
finfo.Delete();
}
}
}