在开发程序中,错误日志很有必要。今天就把使用到的添加错误日志,记录下来,方便以后查看
利用的asp.net错误处理机制
Application_Error
贴出代码
1 ????protected void Application_Error(object sender, EventArgs e) 2 ????????{ 3 ????????????c: Exception Lasterr = Server.GetLastError().GetBaseException();//利用内部服务器对象的GetLastError方法,该方法是返回一个异常, 4 ????????????try 5 ????????????{ 6 ????????????????string path = "/Error/" + DateTime.Today.ToString("yyMMdd") + ".txt"; 7 ????????????????string mapPath = HttpContext.Current.Server.MapPath(path);//利用服务端的mapPath方法,指定虚拟路径相对应的物理路径 8 ??????????????????????????????????????????????????????????????????????????//如果路径下面文件不存在 9 ????????????????if (!File.Exists(mapPath))10 ????????????????{11 ????????????????????File.Create(mapPath).Close();//在该文件夹下面创建文件,并且关闭流,用于释放12 ????????????????}13 ????????????????//实力文件流用于写入14 ????????????????using (StreamWriter writer =File.AppendText(mapPath))//使用apperntext追加到文件中15 ????????????????{16 ????????????????????writer.WriteLine("{0}", DateTime.Now.ToString());17 ????????????????????writer.WriteLine(Lasterr.Message.ToString());18 ????????????????????writer.WriteLine("-------------------------------");19 ????????????????????writer.Flush();//清理缓冲区,并使数据插入20 ????????????????????writer.Close();//写入之后关闭21 ????????????????}22 ????????????}23 ????????????catch(Exception ex)24 ????????????{25 ????????????????goto c;26 ????????????}
asp.net 添加错误日志
原文地址:https://www.cnblogs.com/xinqi1995/p/8254056.html