using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace Common { public class LogHelper { public static string LogInfo = AppDomain.CurrentDomain.BaseDirectory + "Logs\\"; /// /// 获取日志存储路径 /// /// /// 0正常信息,1错误信息 /// private static string GetLogFilePath(string logfilename, int type = 0) { if (!System.IO.Directory.Exists(LogInfo)) { System.IO.Directory.CreateDirectory(LogInfo); } string log = LogInfo + DateTime.Now.ToString("yyyy-MM-dd") + logfilename + ".txt"; if (type != 0) { log = LogInfo + logfilename + ".txt"; } return log; } /// /// 保存的最多日志数量 /// // private static int MaxLogCount = 30; /// /// 日志信息 /// /// /// public static void Info(string msg, string logfilename = "",int type = 0) { try { DeleteLog(); string datapath = GetLogFilePath(logfilename, type); if (type != 0) { using (FileStream fs = new FileStream(datapath, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs)) { //开始写入 sw.WriteLine("当前时间:" + DateTime.Now.ToString()); sw.WriteLine("日志级别:Error"); sw.WriteLine("偏移量:"+ logfilename); sw.WriteLine("日志信息:" + msg); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } return; } if (System.IO.File.Exists(datapath))//追加 { using (FileStream fs = new FileStream(datapath, FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs)) { //开始写入 sw.WriteLine("\n"); sw.WriteLine("当前时间:" + DateTime.Now.ToString()); sw.WriteLine("日志级别:Info"); sw.WriteLine("日志信息:" + msg); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } } else//创建 { using (FileStream fs = new FileStream(datapath, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs)) { //开始写入 sw.WriteLine("当前时间:" + DateTime.Now.ToString()); sw.WriteLine("日志级别:Info"); sw.WriteLine("日志信息:" + msg); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } } } catch { } } /// /// 错误日志 /// /// /// public static void Error(Exception ex, string logfilename = "", int type = 0) { try { DeleteLog(); string datapath = GetLogFilePath(logfilename, type); if (System.IO.File.Exists(datapath))//追加 { using (FileStream fs = new FileStream(datapath, FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs)) { //开始写入 sw.WriteLine("\n"); sw.WriteLine("当前时间:" + DateTime.Now.ToString()); sw.WriteLine("异常级别:Error"); sw.WriteLine("异常信息:" + ex.Message); sw.WriteLine("异常对象:" + ex.Source); sw.WriteLine("异常位置:" + ex.StackTrace); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } } else//创建 { using (FileStream fs = new FileStream(datapath, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs)) { //开始写入 sw.WriteLine("当前时间:" + DateTime.Now.ToString()); sw.WriteLine("异常级别:Error"); sw.WriteLine("异常信息:" + ex.Message); sw.WriteLine("异常对象:" + ex.Source); sw.WriteLine("异常位置:" + ex.StackTrace); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } } } catch { } } /// /// 删除多余日志 /// private static void DeleteLog() { //不必每天都删除日志,整10天删除 if (!(DateTime.Now.Day % 10 == 0)) { return; } DirectoryInfo dic = new DirectoryInfo(LogInfo); if (dic.Exists) { try { //清理10天前的日志文件 dic.GetFiles("*.txt", SearchOption.AllDirectories).Where(n => (DateTime.Now - n.LastWriteTime).TotalDays > 10).ToList().TrueForAll(n => { n.Delete(); return true; }); } catch (Exception ex) { Error(ex); } } } } }