1 /// <summary> 2 /// 创建日志文件,每天一个 3 /// </summary> 4 /// <param name="logContent">日志内容 </param> 5 public static void CreateInLog(string logContent) 6 { 7 string filePath = logurl.Trim();//路径:如 E: 8 filePath = filePath.EndsWith(@"") ? filePath : (filePath + @""); 9 if (Directory.Exists(filePath) == false) 10 { 11 Directory.CreateDirectory(filePath);//判断该路径是否存在,不存在则创建该路径文件夹 12 } 13 14 DateTime dtNow = DateTime.Now;//获取当前日期 15 string dateString = dtNow.Year.ToString(); 16 dateString += dtNow.Month.ToString().Length < 2 ? ("0" + dtNow.Month.ToString()) : (dtNow.Month.ToString()); 17 dateString += dtNow.Day.ToString().Length < 2 ? ("0" + dtNow.Day.ToString()) : (dtNow.Day.ToString()); //将日期格式转成yyyyMMdd的格式,如:20170921 18 19 string filename = filePath + "Log_" + dateString + ".txt"; //创建文件名 20 StreamWriter sw = null; 21 22 if (File.Exists(filename)) 23 { 24 sw = new StreamWriter(filename, true, System.Text.Encoding.GetEncoding("UTF-8")); 25 } 26 else 27 { 28 sw = new StreamWriter(filename, false, System.Text.Encoding.GetEncoding("UTF-8")); 29 } 30 31 StringBuilder dataRow = new StringBuilder(""); 32 dataRow.Append(logContent); 33 sw.WriteLine(dataRow.ToString());//写入内容 34 sw.Close(); 35 }