C#写入log文本

string fname = Directory.GetCurrentDirectory() + "\\LogFile.log";
FileInfo finfo = new FileInfo(fname);
if (!finfo.Exists)
{
FileStream fs;
fs = File.Create(fname);
fs.Close();
finfo = new FileInfo(fname);
}
if (finfo.Length > 1024 * 1024 * 10)
{
/**/
///文件超过10MB则重命名
File.Move(Directory.GetCurrentDirectory() + "\\LogFile.log", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\\LogFile.log");
/**/
///删除该文件
//finfo.Delete();
}
using (FileStream fs = finfo.OpenWrite())
{
/**/
///根据上面创建的文件流创建写数据流
StreamWriter w = new StreamWriter(fs);
w.BaseStream.Seek(0, SeekOrigin.End);
w.Write("{0} {1} \n\r", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());
var sele = db.NikeDTCDailyReport.Where(p => true).ToList();
foreach (var item in sele)
{
w.WriteLine(item);
}

 

w.WriteLine(sele);
w.Flush();
w.Close();
}

posted @ 2016-05-07 01:16  Jesh_95  阅读(1381)  评论(0编辑  收藏  举报