using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace CLOUDWEB { public class Log { public static void WriteListToTextFile(List list) { string uploadPath = System.Web.HttpContext.Current.Server.MapPath("./") + "Log\\"; if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } string fileName = DateTime.Now.ToString("yyyy-MM-dd") + "_sql.log"; string txtFile = uploadPath + fileName; //创建一个文件流,用以写入或者创建一个StreamWriter FileStream fs = new FileStream(txtFile, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.Flush(); // 使用StreamWriter来往文件中写入内容 sw.BaseStream.Seek(0, SeekOrigin.Begin); for (int i = 0; i < list.Count; i++) sw.WriteLine(list[i]); //关闭此文件t sw.Flush(); sw.Close(); fs.Close(); } public static void LogInfo(string msg) { string uploadPath = System.Web.HttpContext.Current.Server.MapPath("./") + "Log\\"; if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } string fileName = DateTime.Now.Ticks.ToString() + ".log"; FileStream fs; StreamWriter sw; string sFileName = uploadPath + fileName; if (File.Exists(sFileName)) //验证文件是否存在,有则追加,无则创建 { fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write); } else { fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write); } sw = new StreamWriter(fs); msg = String.Format("------------------------{0}------------------------------\r\n", DateTime.Now) + msg; msg += "\r\n"; sw.WriteLine(msg); sw.Close(); fs.Close(); } public static void LogSaveInfo(string msg) { string uploadPath = System.Web.HttpContext.Current.Server.MapPath("./") + "Log\\"; if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } string fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".log"; FileStream fs; StreamWriter sw; string sFileName = uploadPath + fileName; if (File.Exists(sFileName)) //验证文件是否存在,有则追加,无则创建 { fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write); } else { fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write); } sw = new StreamWriter(fs); msg = String.Format("------------------------{0}------------------------------\r\n", DateTime.Now) + msg; msg += "\r\n"; sw.WriteLine(msg); sw.Close(); fs.Close(); } } }