using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; namespace Maticsoft.Common { /// <summary> /// /// </summary> public class LogHelper { /// <summary> /// /// </summary> /// <param name="FileName">文件名 log.txt </param> /// <param name="str">日志内容</param> public static void CreateTxt(string FileName,string str) { string businessCode = ""; try { businessCode=HttpContext.Current.Session[Globals.BusinessCode_String].ToString(); } catch (Exception) { businessCode = "无"; } string strPath = AppDomain.CurrentDomain.BaseDirectory + "sysLog\" + businessCode + "\" + DateTime.Now.ToString("yyyyMM") + "\"; if (Directory.Exists(strPath) == false)//如果不存在就创建file文件夹 { Directory.CreateDirectory(strPath); } string filePath = strPath + "/" + DateTime.Now.ToString("yyyyMMdd") + FileName; StreamWriter sw; if (!File.Exists(filePath)) { //不存在就新建一个文本文件,并写入一些内容 sw = File.CreateText(filePath); sw.WriteLine(" 当前日期是:" + DateTime.Now); sw.WriteLine(str); sw.WriteLine(" "); } else { //如果存在就添加一些文本内容 sw = File.AppendText(filePath); sw.WriteLine(" 当前日期是:" + DateTime.Now); sw.WriteLine(str); sw.WriteLine(" "); } sw.Close(); } } }