protected void ApplicationEndRequest()
        {
            var responseStatusCode = (HttpStatusCode)Response.StatusCode;
            var lastError          = Server.GetLastError();

            // Decide about logging the error or not
            // -------------------------------------
            if (HttpStatusCodesThatShouldBeLogged.Contains(responseStatusCode) && SitkaHttpRequestStorage.ShouldLogErrorFromApplicationEnd)
            {
                var requestInfo = new SitkaRequestInfo(lastError, HttpContext.Current);
                if (!IsRequestExemptedFromLogging(requestInfo))
                {
                    if (lastError == null && IsWebServiceHttpRequest(Request))
                    {
                        lastError = SitkaHttpRequestStorage.WcfStoredError;
                    }
                    SitkaLogger.Instance.LogDetailedErrorMessage(string.Format("Http Server is returning response status code \"{0}\" and error was not logged via the Application_Error method", Response.Status), lastError, Context);
                    if (lastError != null && lastError.InnerException != null)
                    {
                        SitkaLogger.Instance.LogDetailedErrorMessage(string.Format("Http Server is returning response status code \"{0}\" - Inner exception", Response.Status), lastError.InnerException, Context);
                    }
                }
            }

            // Send the NotFound page as needed
            // --------------------------------
            if (responseStatusCode == HttpStatusCode.NotFound || SitkaHttpRequestStorage.NotFoundStoredError != null)
            {
                var html = Instance.NotFoundHtml;
                if (SitkaHttpRequestStorage.NotFoundStoredError != null)
                {
                    html += string.Format("<p>{0}</p>", SitkaHttpRequestStorage.NotFoundStoredError.Message);
                    SitkaHttpRequestStorage.NotFoundStoredError = null;
                }
                var errorHtml = GetMissingPageHtml(Request.Cookies.ToCookieCollection(Request.Url.Host), html);
                SendErrorResponseToBrowser(errorHtml, (int)HttpStatusCode.NotFound);
            }

            // Release any resources needed only for this one http request, particularly any EntityFramework contexts or other IDisposable items
            SitkaHttpRequestStorage.DisposeItemsAndClearStore();
        }
        protected void ApplicationError()
        {
            string errorPageHtml;
            var    httpStatusCode  = (int)HttpStatusCode.InternalServerError;
            var    lastStoredError = Server.GetLastError();
            var    requestInfo     = new SitkaRequestInfo(lastStoredError, HttpContext.Current);

            try
            {
                if (!IsRequestExemptedFromLogging(requestInfo))
                {
                    SitkaLogger.Instance.LogDetailedErrorMessage(requestInfo);
                }

                if (lastStoredError is HttpException)
                {
                    httpStatusCode = ((HttpException)lastStoredError).GetHttpCode();
                }

                if (lastStoredError is SitkaDisplayErrorExceptionWithHttpCode)
                {
                    httpStatusCode = (int)((SitkaDisplayErrorExceptionWithHttpCode)lastStoredError).HttpStatusCode;
                }

                if (httpStatusCode == (int)HttpStatusCode.NotFound || SitkaHttpRequestStorage.NotFoundStoredError != null)
                {
                    return; // we know that applicationend will spit out the not found error page to the client.
                }

                errorPageHtml = GetErrorPageHtml(Request.IsLocal, Request.Cookies.ToCookieCollection(Request.Url.Host), lastStoredError);
            }
            catch (Exception ex)
            {
                // Catch and log anything that goes wrong in this error handler, we don't let it escape so that it doesn't start the error handling over again
                SitkaLogger.Instance.LogDetailedErrorMessage(new Exception("Secondary exception occurred while trying to display error message.", ex));
                errorPageHtml = WrapExceptionDetailsInHtml(String.Format("Secondary exception occurred while trying to display error message, original error message lost.\r\nSecondary Exception Details:\r\n{0}", ex));
            }

            SendErrorResponseToBrowser(errorPageHtml, httpStatusCode);
            Server.ClearError();
        }
 private bool IsRequestExemptedFromLogging(SitkaRequestInfo requestInfo)
 {
     return(LoggingFilters.Any(sitkaLoggingFilter => sitkaLoggingFilter.ShouldRequestBeFiltered(requestInfo)));
 }
Beispiel #4
0
 public void LogDetailedErrorMessage(SitkaRequestInfo requestInfo)
 {
     Logger.Error(requestInfo.ToString(), requestInfo.OriginalException);
     SitkaGlobalBase.CancelErrorLoggingFromApplicationEnd();
 }