首页  ·  知识 ·  
Label
      编辑:  图片来源:网络
1.第一种做法,在Web.config文件配置
<system.web>
      <customErrors defaultRedirect="~/ErrorPage.aspx"
                     mode="RemoteOnly">
      </customErrors>
</system.web>
 
  defaultRedirect属性用来指明当aspx页面发生了未处理错误时导向的页面; 但Asp.net使用重定向机制来重新导航错误页面,这样错误信息就会丢失,也就是说我们用Server.GetLastError()获得的Exception对象始终是空的。虽然可以提示用户出错,并提供一个返回出错页面的链接,却不能给管理员一个很好的错误提示。
2.第二种做法:在global文件里的Application_Error方法中处理
代码
protected void Application_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.Transfer("~/ErrorPage.aspx"); //跳转到显示友好错误的页面
        }
然后在ErrorPage.aspx页面显示一些好友的提示信息.
3.第三种做法:在Page_Error事件里面处理
代码
        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");
           
        }
 
经常的做法是使用第二种方法,然后再写一个发送短信的方法(调用移动的短信借口),这样的话程序出错的时候,管理员可以收到程序出错的信息。
 
本文作者:liguangxi8 来源: http://www.cnblogs.com/liguangxi8/archive/2010/05/17/1737462.html
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的
收藏至微信