Ejemplo n.º 1
0
        protected void Application_Error(object sender, EventArgs e)
        {
            var context = HttpContext.Current;
            if (context != null)
            {
                Exception ex = HttpContext.Current.Server.GetLastError();
                //if (ex.Message == "授权已到期,请联系管理员。")
                //{
                //    context.Response.Redirect("~/Login.html");
                //    return;
                //}

                var logservice = new ErrorLogService();
                var memberservice = new MemberService();
                int uid = Convert.ToInt32(context.User.Identity.Name);
                var member = memberservice.FirstOrDefault(m => m.id == uid);
               
                var errorlog = new sys_ErrorLog
                {
                    uid = member.id,
                    RealName = member.RealName,
                    LogContent = ex.InnerException.Message,
                    Ip = GetIP,
                    LogTime = DateTime.Now
                };
                logservice.Add(errorlog);

                var errorcontent = new StringBuilder();
                errorcontent.AppendLine(string.Format("<b>发生时间:</b>{0}<br/><br/>", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")));
                errorcontent.AppendLine(string.Format("<b>错误描述:</b>{0}<br/><br/>", ex.Message.Replace("\r\n", "")));
                errorcontent.AppendLine(string.Format("<b>错误对象:</b>{0}<br/><br/>", ex.Source));
                errorcontent.AppendLine(string.Format("<b>错误页面:</b>{0}<br/><br/>", HttpContext.Current.Request.Url));
                errorcontent.AppendLine(string.Format("<b>浏览器IE:</b>{0}<br/><br/>", HttpContext.Current.Request.UserAgent));
                errorcontent.AppendLine(string.Format("<b>服务器IP:</b>{0}<br/><br/>", HttpContext.Current.Request.ServerVariables.Get("Local_Addr")));
                errorcontent.AppendLine(string.Format("<b>方法名称:</b>{0}<br/><br/>", ex.TargetSite));
                errorcontent.AppendLine(string.Format("<b>C#类名称:</b>{0}<br/><br/>", ex.TargetSite.DeclaringType));
                errorcontent.AppendLine(string.Format("<b>成员变量:</b>{0}<br/><br/>", ex.TargetSite.MemberType));

                Cookie.SetObj("ERRORINFO", errorcontent.ToString());
                Server.ClearError();

                //出错画面处理
                context.Response.Redirect("~/ErrorPage.aspx");
            }
        }