public static void WriteErorrLog(Exception ex) { if(ex == null) return; //ex = null 返回 StreamWrite write = null; DateTime dt = DateTime.Now; // 设置日志时间 string time = dt.ToString("yyyy-mm-dd HH:mm:ss")); //年-月-日 时:分:秒 string LogName = "Log.log"; //日志名称 string LogPath = "C://Error//"; //日志存放路径 string Log = LogPath + LogName; //路径 + 名称 if(!File.Exists(Log)) //是否存在 { Directory.CreateDirectory(LogPath); //创建文件夹 write = File.CreateText(LogName); // 创建日志 } else { write = File.AppendText(Log); //追加,添加错误信息; } write.WriteLine(time); write.WriteLine(ex.Message); write.WriteLine("异常信息:"+ex.ToString()); write.WriteLine("异常堆栈:"+ex.StackTrace.ToString()); write.WriteLine("/r/n-----------------"); write.Flush(); write.Dispse(); }