?
【网上购物论坛】-IT泡吧![Www.itpob.Cn]网上购物社区! - Powered by Discuz!
http://www.itpob.cn/
?
一种做法,在Web.config文件配置
??????<customErrors?defaultRedirect="~/ErrorPage.aspx"?
?????????????????????mode="RemoteOnly">
??????</customErrors>
</system.web>
?
? defaultRedirect属性用来指明当aspx页面发生了未处理错误时导向的页面; 但Asp.net使用重定向机制来重新导航错误页面,这样错误信息就会丢失,也就是说我们用Server.GetLastError()获得的Exception对象始终是空的。虽然可以提示用户出错,并提供一个返回出错页面的链接,却不能给管理员一个很好的错误提示。
2.第二种做法:在global文件里的Application_Error方法中处理
? ?代码
????????{
????????????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");
????????????
????????}