.net页面出错时的处理方法?【网上购物论坛】-IT泡吧![Www.itpob.Cn]网上购物社区! - Powered by Discuz!http
.net页面出错时的处理方法
?
【网上购物论坛】-IT泡吧![Www.itpob.Cn]网上购物社区! - Powered by Discuz!
http://www.itpob.cn/
?
一种做法,在Web.config文件配置
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->????????private?void?Page_Load(object?sender,?System.EventArgs?e)
????????{
????????????throw(new?ArgumentNullException());
????????}
????????public?void?Page_Error(object?sender,EventArgs?e)
????????{
????????????Exception?ex=Server.GetLastError().GetBaseException();
????????????string?errorTime="发生时间:"+DateTime.Now.ToString();
????????????string?errorAddress="发生异常页:"+Request.Url.ToString();
????????????string?errorInfo="异常信息:"+ex.Message;
????????????string?errorSource="错误源:"+ex.Source;
????????????string?errorTrace="堆栈信息:"+ex.StackTrace;
????????????Server.ClearError();
????????????System.IO.StreamWriter?writer=null;
????????????try
????????????{
????????????????lock(this)
????????????????{
????????????????????//写入日志?
????????????????????string?year=DateTime.Now.Year.ToString();
????????????????????string?month=DateTime.Now.Month.ToString();
????????????????????string?day=DateTime.Now.Day.ToString();
????????????????????string?path=string.Empty;
????????????????????string?filename=DateTime.Now.ToString("yyyyMMdd")+".txt";
????????????????????path=Server.MapPath("~/Error/")+year+month+day;
????????????????????if(!Directory.Exists(path))
????????????????????{
????????????????????????Directory.CreateDirectory(path);
????????????????????}
????????????????????System.IO.FileInfo?file=new?FileInfo(path+"/"+filename);
????????????????????writer=new?StreamWriter(file.FullName,true);//文件不在则创建,true表示追加
????????????????????writer.WriteLine("用户IP:"+Request.UserHostAddress);
????????????????????writer.WriteLine(errorTime);
????????????????????writer.WriteLine(errorAddress);
????????????????????writer.WriteLine(errorInfo);
????????????????????writer.WriteLine(errorSource);
????????????????????writer.WriteLine(errorTrace);
????????????????????writer.WriteLine("-------------------------------------------");
????????????????}
????????????}
????????????finally
????????????{
????????????????if(writer!=null)
????????????????{
????????????????????writer.Close();
????????????????}
????????????}
????????????Server.ClearError();//防止错误继续到要被处理的?Application_Error?事件中。
????????????Response.Redirect("~/ErrorPage.aspx");
????????????
????????}
?