写日志

string DocmentPath =Server.MapPath("Log/log1.txt");
        string memo = "日志内容";
        System.Text.Encoding enc = System.Text.Encoding.GetEncoding("gb2312");
        StreamWriter sw = new StreamWriter(DocmentPath, false, enc);
        sw.WriteLine(memo);
        sw.Close();

 

 

补充一个比较全方式,文件存在则写入,不存在则先创建文件在写入

 

string fileName = DateTime.Now.ToShortDateString();
FileStream fs = new FileStream(@"d:\"+ fileName + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(string.Format("日志写入时间 {0}", DateTime.Now.ToString()));
m_streamWriter.WriteLine("ddddddddddd");
m_streamWriter.Flush();
m_streamWriter.Close();

posted @ 2011-09-08 07:47  weide  阅读(175)  评论(0编辑  收藏  举报