예제 #1
0
        protected void Application_Error(object sender, EventArgs e)
        {
            StringBuilder errLog = null;
            Exception     ex     = null;

            Model.Sys_ErrLogInfo newErr = new Model.Sys_ErrLogInfo();
            newErr.ErrLogId = SQLHelper.GetNewID(typeof(Model.Sys_ErrLogInfo));
            try
            {
                // 获取错误类
                ex = Server.GetLastError().InnerException;
                if (ex == null)
                {
                    ex = Server.GetLastError().GetBaseException();
                }
                errLog = new StringBuilder();
                errLog.Append(String.Format(CultureInfo.InvariantCulture, "出错文件:{0}\r\n", Request.Url.AbsoluteUri));
                newErr.ErrUrl = Request.Url.AbsoluteUri;

                if (Request.UserHostAddress != null)
                {
                    errLog.Append(String.Format(CultureInfo.InvariantCulture, "IP地址:{0}\r\n", Request.UserHostAddress));
                    newErr.ErrIP = Request.UserHostAddress;
                }

                if (Session != null && Session["CurrUser"] != null)
                {
                    errLog.Append(String.Format(CultureInfo.InvariantCulture, "操作人员:{0}\r\n", ((Model.Sys_User)Session["CurrUser"]).UserName));
                    newErr.UserName = ((Model.Sys_User)Session["CurrUser"]).UserId;
                }
                else
                {
                    PageBase.ZXRefresh(Request.ApplicationPath + "/LogOff.aspx");
                }
            }
            catch
            {
                try
                {
                    PageBase.ZXRefresh(Request.ApplicationPath + "/OperationError.aspx");
                }
                catch
                {
                }
            }
            finally
            {
                if (errLog != null)
                {
                    Funs.DB.Sys_ErrLogInfo.InsertOnSubmit(newErr);
                    Funs.DB.SubmitChanges();
                }

                ErrLogInfo.WriteLog(newErr.ErrLogId, ex, errLog == null ? null : errLog.ToString());
                Server.ClearError();

                PageBase.ZXRefresh(Request.ApplicationPath + "/OperationError.aspx");
            }
        }
예제 #2
0
        /// <summary>
        /// 用默认错误日志路径写入错误日志
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="appendErrLog">异常后追加的说明信息</param>
        /// <returns>最终写入的错误信息</returns>
        public static string WriteLog(string ErrId, Exception ex, params string[] appendErrLog)
        {
            StringBuilder errLog = new StringBuilder();

            if (ex != null)
            {
                Exception innerEx = ex.InnerException;
                errLog.Append("\r\n");
                errLog.Append("错误信息开始=====>\r\n");
                errLog.Append(String.Format(CultureInfo.InvariantCulture, "错误类型:{0}\r\n", ex.GetType().Name));
                errLog.Append(String.Format(CultureInfo.InvariantCulture, "错误信息:{0}\r\n", ex.Message));
                errLog.Append("错误堆栈:\r\n");
                errLog.Append(String.Format(CultureInfo.InvariantCulture, "{0}\r\n", ex.StackTrace));
                Model.Sys_ErrLogInfo newErr = new Model.Sys_ErrLogInfo();
                newErr.ErrType       = ex.GetType().Name;
                newErr.ErrMessage    = ex.Message;
                newErr.ErrStackTrace = ex.StackTrace;

                int level = 1;
                while (innerEx != null)
                {
                    string preString = new string('-', level * 4);
                    errLog.Append(preString);
                    if (innerEx.GetType() != null)
                    {
                        errLog.Append(String.Format(CultureInfo.InvariantCulture, "错误类型:{0}\r\n", innerEx.GetType().Name));
                    }
                    errLog.Append(preString);
                    errLog.Append("错误信息:\r\n");
                    errLog.Append(preString);
                    errLog.Append(innerEx.Message + "\r\n");
                    errLog.Append(preString);
                    errLog.Append("错误堆栈:\r\n");
                    errLog.Append(new string(' ', level * 4));
                    if (innerEx.StackTrace != null)
                    {
                        errLog.Append(String.Format(CultureInfo.InvariantCulture, "{0}\r\n", innerEx.StackTrace.Replace("\r\n", "\r\n" + new string(' ', level * 4))));
                    }
                    newErr.ErrType       += innerEx.GetType().Name;
                    newErr.ErrMessage    += innerEx.Message;
                    newErr.ErrStackTrace += innerEx.StackTrace;

                    innerEx = innerEx.InnerException;
                    level++;
                }

                errLog.Append(String.Format(CultureInfo.InvariantCulture, "出错时间:{0}\r\n", DateTime.Now));
                newErr.ErrTime = DateTime.Now;

                if (!string.IsNullOrEmpty(ErrId))
                {
                    var errlogInfo = Funs.DB.Sys_ErrLogInfo.FirstOrDefault(x => x.ErrLogId == ErrId);
                    if (errlogInfo != null)
                    {
                        errlogInfo.ErrType       = newErr.ErrType;
                        errlogInfo.ErrMessage    = newErr.ErrMessage;
                        errlogInfo.ErrStackTrace = newErr.ErrStackTrace;
                        errlogInfo.ErrTime       = newErr.ErrTime;
                        Funs.DB.SubmitChanges();
                    }
                }
                else
                {
                    newErr.ErrLogId = SQLHelper.GetNewID(typeof(Model.Sys_ErrLogInfo));
                    Funs.DB.Sys_ErrLogInfo.InsertOnSubmit(newErr);
                    Funs.DB.SubmitChanges();
                }
            }

            if (appendErrLog != null)
            {
                foreach (string log in appendErrLog)
                {
                    if (log != null)
                    {
                        errLog.Append(log);
                        errLog.Append("\r\n");
                    }
                }

                errLog.Append(String.Format(CultureInfo.InvariantCulture, "出错时间:{0}\r\n", DateTime.Now));
            }

            return(WriteLog(errLog.ToString()));
        }