System.IO.StreamWriter w = new System.IO.StreamWriter(Server.MapPath("~/App_data/xxx.log"), true);
w.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " IP:" + clientIp + " UserName:" + this.Login1.UserName);
w.Close();
1 using System.IO; 2 public static void WriteLog(string strLog) 3 { 4 string sFilePath="d:\"+DateTime.Now.ToString("yyyyMM"); 5 string sFileName = "rizhi" + DateTime.Now.ToString("dd") + ".log"; 6 sFileName = sFilePath+ "\"+sFileName; //文件的绝对路径 7 if (!Directory.Exists(sFilePath))//验证路径是否存在 8 { 9 Directory.CreateDirectory(sFilePath); 10 //不存在则创建 11 } 12 FileStream fs; 13 StreamWriter sw; 14 if (File.Exists(sFileName)) 15 //验证文件是否存在,有则追加,无则创建 16 { 17 fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write); 18 } 19 else 20 { 21 fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write); 22 } 23 sw = new StreamWriter(fs); 24 sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " --- " + strLog); 25 sw.Close(); 26 fs.Close(); 27 }