C#简单写日志
public static string LogDirectory = GetDirectoryName(Assembly.GetAssembly(typeof(LogManager))) + "\\Log"; //public static string LogDirectory = AppDomain.CurrentDomain.BaseDirectory + "Log\\"; public static string GetAssemblyPath(Type type) { return GetAssemblyPath(type.Assembly); } public static string GetDirectoryName(Assembly assembly) { return Path.GetDirectoryName(GetAssemblyPath(assembly)); } public static string GetAssemblyPath(Assembly assembly) { string escapedCodeBase = assembly.EscapedCodeBase; if (IsFileUri(escapedCodeBase)) { return GetAssemblyPathFromEscapedCodeBase(escapedCodeBase); } return assembly.Location; } public static string GetAssemblyPathFromEscapedCodeBase(string escapedCodeBase) { return new Uri(escapedCodeBase).LocalPath; } private static bool IsFileUri(string uri) { return uri.ToLower().StartsWith(Uri.UriSchemeFile); } /// <summary> /// 保存的最多日志数量 /// </summary> private static int MaxLogCount = 365; /// <summary> /// /// </summary> /// <param name="msg"></param> /// <param name="logfilename">日志文件名称</param> public static void Write(string msg, string logfilename = "") { try { DeleteLog(); string datapath = GetLogFilePath(logfilename); 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("日志信息:" + 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("日志信息:" + msg); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } } } catch { } } public static void Write(Exception ex, string logfilename = "") { try { DeleteLog(); string datapath = GetLogFilePath(logfilename); 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("异常信息:" + ex.Message); sw.WriteLine("异常对象:" + ex.Source); sw.WriteLine("调用堆栈:" + ex.StackTrace); sw.WriteLine("消息:" + ex.InnerException.Message); sw.WriteLine("触发方法:" + ex.TargetSite); //清空缓冲区 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("异常信息:" + ex.Message); sw.WriteLine("异常对象:" + ex.Source); sw.WriteLine("调用堆栈:" + ex.StackTrace); sw.WriteLine("消息:" + ex.InnerException.Message); sw.WriteLine("触发方法:" + ex.TargetSite); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } } } catch { } } public static void Write(object obj, string logfilename = "") { Write(obj.ToString()); } /// <summary> /// 获取日志存储路径 /// </summary> /// <returns></returns> private static string GetLogFilePath(string logfilename) { string filepath = ""; if (!System.IO.Directory.Exists(LogDirectory)) { System.IO.Directory.CreateDirectory(LogDirectory); } filepath = LogDirectory + DateTime.Now.ToString("yyyy-MM-dd") + "_" + logfilename + ".txt"; return filepath; } /// <summary> /// 删除多余日志 /// </summary> private static void DeleteLog() { DirectoryInfo dic = new DirectoryInfo(LogDirectory); //不必每天都删除日志,整10天删除 if (!(DateTime.Now.Day % 10 == 0)) { return; } if (dic.Exists) { var files = dic.GetFiles(); if (files.Length < MaxLogCount) return; List<int> names = new List<int>(); for (int i = 0; i < files.Length; i++) { string strname = Path.GetFileNameWithoutExtension(files[i].Name); int name; if (int.TryParse(strname, out name)) { names.Add(name); } } names.Sort();//升序 //names.Reverse();//降序 for (int i = 0; i < names.Count - MaxLogCount; i++) { string path = System.IO.Path.Combine(LogDirectory, names[i].ToString() + ".txt"); File.Delete(path); } } }