You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text;
namespace ICSSoft.ERPWMS.SQL { public class Log { public static void WriteLogFile(string input, string txtName) { try { string logAdress = "D:" + "\\Log\\"; if (!System.IO.Directory.Exists(logAdress)) { System.IO.Directory.CreateDirectory(logAdress);//不存在就创建目录
}
string adress = logAdress + txtName; if (!System.IO.Directory.Exists(adress)) { System.IO.Directory.CreateDirectory(adress);//不存在就创建目录
}
// string logAdress = ConfigurationManager.AppSettings["logAdress"].ToString();
/**/ ///指定日志文件的目录
string fname = adress + "\\" + "log" + DateTime.Now.ToString("yy-MM-dd") + ".txt"; /**/ ///定义文件信息对象
FileInfo finfo = new FileInfo(fname);
if (!finfo.Exists) { FileStream fs; fs = File.Create(fname); fs.Close(); finfo = new FileInfo(fname); }
/**/ ///判断文件是否存在以及是否大于2K
if (finfo.Length > 1024 * 1024 * 10) { /**/ ///文件超过10MB则重命名
File.Move(logAdress + "\\Log\\" + txtName + ".txt", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\\Log\\" + txtName + ".txt"); /**/ ///删除该文件
//finfo.Delete();
} //finfo.AppendText();
/**/ ///创建只写文件流
using (FileStream fs = finfo.OpenWrite()) { /**/ ///根据上面创建的文件流创建写数据流
StreamWriter w = new StreamWriter(fs);
/**/ ///设置写数据流的起始位置为文件流的末尾
///设置写数据流的起始位置为文件流的末尾
w.BaseStream.Seek(0, SeekOrigin.End);
w.WriteLine("*****************Start*****************"); w.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); /**/ ///写入当前系统时间并换行
/**/ ///写入日志内容并换行
w.WriteLine(input);
/**/ ///写入------------------------------------“并换行
w.WriteLine("------------------END------------------------");
/**/ ///清空缓冲区内容,并把缓冲区内容写入基础流
w.Flush();
/**/ ///关闭写数据流
w.Close(); } } catch (Exception ex) { }
} } }
|