示例#1
0
文件: Helper.cs 项目: micajah/Error
        public static void SendError(Exception ex)
        {
            try
            {
                if (null == HttpContext.Current)
                {
                    return;
                }

                ErrorTracker.ErrorInfo oErrorInfo = new ErrorTracker.ErrorInfo();
                ErrorTracker.Service   oService   = new Micajah.ErrorTrackerHelper.ErrorTracker.Service();

                #region Collecting ErrorInfo Data

                oErrorInfo.ApplicationID = Convert.ToInt32(ConfigurationSettings.AppSettings["ApplicationID"]);

                try
                {
                    WebException.GetExceptions(ex, oErrorInfo);
                }
                catch (ApplicationStartException)
                {
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.Buffer = false;
                    HttpContext.Current.Response.Write("Error, Site is restarting. Try again later.");
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.End();
                    return;
                }

                oErrorInfo.Browser          = System.Web.HttpContext.Current.Request.Browser.Browser;
                oErrorInfo.PhysicalFileName = System.Web.HttpContext.Current.Request.PhysicalPath;
                oErrorInfo.UserIPAddress    = System.Web.HttpContext.Current.Request.UserHostAddress.ToString();
                oErrorInfo.UserHostName     = System.Web.HttpContext.Current.Request.UserHostName.ToString();
                oErrorInfo.ErrorFile        = System.Web.HttpContext.Current.Request.Url.LocalPath;
                oErrorInfo.URL                    = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
                oErrorInfo.URLReferrer            = null != System.Web.HttpContext.Current.Request.UrlReferrer ? System.Web.HttpContext.Current.Request.UrlReferrer.AbsoluteUri : "None";
                oErrorInfo.MachineName            = System.Web.HttpContext.Current.Server.MachineName.ToString();
                oErrorInfo.Version                = WebException.GetVersionNumbers();
                oErrorInfo.StackTrace             = WebException.GetTrace();
                oErrorInfo.Form                   = WebException.GetForm();
                oErrorInfo.Session                = WebException.GetSession();
                oErrorInfo.QueryString            = System.Web.HttpContext.Current.Request.QueryString.ToString();
                oErrorInfo.QueryStringDescription = WebException.GetQueryString();
                oErrorInfo.ApplicationDescription = WebException.GetApplication();
                oErrorInfo.RequestCookies         = WebException.GetRequestCookies();
                oErrorInfo.ResponseCookies        = WebException.GetResponseCookies();
                oErrorInfo.ServerVariables        = WebException.GetServerVariables();
                oErrorInfo.Cache                  = WebException.GetCache();

                #endregion

                oService.AddError(oErrorInfo);
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
示例#2
0
        public static void GetExceptions(Exception oCurrentException, ErrorTracker.ErrorInfo oErrorInfo)
        {
            string        sFullTrace        = string.Empty;
            string        strInnerErrorType = string.Empty;
            string        strErrorTrace     = string.Empty;
            string        strErrorLine      = string.Empty;
            string        strErrorFile      = string.Empty;
            string        strErrorMessage   = string.Empty;
            string        strErrorPage      = System.Web.HttpContext.Current.Request.PhysicalPath;
            StringBuilder oSB = new StringBuilder();

            AppendTableHeader(oSB);

            oSB.Append("<tr>");
            oSB.Append("<td colspan=2><h3>Exceptions</h3></td>");
            oSB.Append("</tr>");

            bool bFirstStepDown = true;

            while (oCurrentException != null)
            {
                Exception exInnerError = oCurrentException;
                if (exInnerError != null)
                {
                    strInnerErrorType = exInnerError.GetType().ToString();
                    switch (strInnerErrorType)
                    {
                    // ascx/aspx compile error
                    case "System.Web.HttpCompileException":
                        System.CodeDom.Compiler.CompilerErrorCollection colErrors = ((System.Web.HttpCompileException)exInnerError).Results.Errors;
                        if (colErrors.Count > 0)
                        {
                            strErrorLine    = colErrors[0].Line.ToString();
                            strErrorFile    = colErrors[0].FileName;
                            strErrorMessage = colErrors[0].ErrorNumber + ": " + colErrors[0].ErrorText;
                        }
                        break;

                    // any other error like XML parsing or bad string manipulations
                    default:
                        System.Diagnostics.StackTrace stError = new System.Diagnostics.StackTrace(exInnerError, true);
                        for (int i = 0; i < stError.FrameCount; i++)
                        {
                            if (stError.GetFrame(i).GetFileName() != null)
                            {
                                strErrorLine    = stError.GetFrame(i).GetFileLineNumber().ToString();
                                strErrorFile    = stError.GetFrame(i).GetFileName();
                                strErrorMessage = exInnerError.Message;
                                break;
                            }
                        }
                        if (strErrorFile == "")
                        {
                            strErrorMessage = "Untrapped Exception: " + exInnerError.Message;
                            strErrorFile    = "Unknown";
                        }
                        break;
                    }
                    strErrorTrace = exInnerError.StackTrace;
                    sFullTrace   += strErrorTrace;
                }
                else
                {
                    strErrorMessage = oCurrentException.Message;
                    strErrorTrace   = oCurrentException.StackTrace;
                    strErrorFile    = "Unknown";
                }

                if (strErrorMessage.IndexOf("Application is restarting") > -1)
                {
                    throw new ApplicationStartException();
                }

                if ((strInnerErrorType != null) && (strInnerErrorType.Trim().Length > 0))
                {
                    AppendTableRow(oSB, "Type", strInnerErrorType.ToString(), true);
                }

                if ((strErrorMessage != null) && (strErrorMessage.Trim().Length > 0))
                {
                    AppendTableRow(oSB, "Message", strErrorMessage.ToString(), true, "color:red;");
                }

                if ((strErrorFile != null) && (strErrorFile.Trim().Length > 0))
                {
                    AppendTableRow(oSB, "Error File", CreateAnchor(strErrorFile.ToString()), true);
                }

                if ((strErrorLine != null) && (strErrorLine.Trim().Length > 0))
                {
                    AppendTableRow(oSB, "Error Line", strErrorLine.ToString(), true);
                }

                if ((strErrorTrace != null) && (strErrorTrace.Trim().Length > 0))
                {
                    AppendTableRow(oSB, "StackTrace", strErrorTrace.ToString().ToString().Replace("\n", "<br />"), true);
                }

                oSB.Append("<tr>");
                oSB.Append("<td colspan2>&nbsp;</td>");
                oSB.Append("</tr>");

                if (bFirstStepDown)
                {
                    oErrorInfo.Name            = strInnerErrorType;
                    oErrorInfo.ExceptionType   = strInnerErrorType;
                    oErrorInfo.ErrorLineNumber = string.Empty == strErrorLine ? 0 : Convert.ToInt32(strErrorLine);
                    oErrorInfo.SourceFile      = strErrorFile;
                    oErrorInfo.Description     = strErrorMessage;
                }

                oCurrentException = oCurrentException.InnerException;

                bFirstStepDown = false;
            }
            AppendTableFooter(oSB);
            AppendHr(oSB);

            oErrorInfo.ExceptionsDescription = oSB.ToString();
        }