Пример #1
0
    public static void Exception(Exception exception)
    {
        StringBuilder sb      = new StringBuilder();
        string        source  = null;
        string        message = null;

        while (exception != null)
        {
            source  = exception.Source;
            message = exception.Message;

            foreach (string ignoredException in IGNORED_EXCEPTIONS)
            {
                if (message.IndexOf(ignoredException, StringComparison.InvariantCulture) >= 0)
                {
                    return;
                }
            }

            sb.Append("\r\n-------------------------------------\r\n");
            sb.AppendFormat("Exception:{0}\r\n", exception.Message);
            sb.AppendFormat("Source:{0}\r\n", exception.Source);
            try
            {
                sb.AppendFormat("DateTime:{0}\r\n", DateTime.Now);
                if (HttpContext.Current != null)
                {
                    if (HttpContext.Current.Request != null)
                    {
                        if (HttpContext.Current.Request.HttpMethod.Equals("HEAD"))
                        {
                            return; // ignore the HEAD request
                        }
                        sb.AppendFormat("HTTP Method:{0}\r\n", HttpContext.Current.Request.HttpMethod);
                        sb.AppendFormat("URL:{0}\r\n", HttpContext.Current.Request.Url);
                        sb.AppendFormat("IP:{0}\r\n", HttpContext.Current.Request.GetRealUserAddress());
                        if (HttpContext.Current.Request.UrlReferrer != null)
                        {
                            sb.AppendFormat("Referrer Url:{0}\r\n", HttpContext.Current.Request.UrlReferrer);
                        }
                        if (HttpContext.Current.Request.UserAgent != null)
                        {
                            sb.AppendFormat("User Agent:{0}\r\n", HttpContext.Current.Request.UserAgent);
                        }
                    }

                    sb.AppendFormat("UserID:{0}\r\n", CustomProfile.Current.UserID);
                    sb.AppendFormat("UserName:{0}\r\n", CustomProfile.Current.UserName);
                    sb.AppendFormat("SessionID:{0}\r\n", CustomProfile.Current.SessionID);
                }
            }
            catch
            {
            }
            sb.AppendFormat("Stack Trace:\r\n{0}\r\n", exception.StackTrace);

            exception = exception.InnerException;
        }

        LogEntry log = new LogEntry()
        {
            Message    = message,
            StackTrace = sb.ToString(),
            Source     = source,
        };

        ExceptionHandler.AppendToEmail(sb.ToString());
        Append(log, LogType.Exception);
    }