Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Error"/> class
        /// from a given <see cref="Exception"/> instance and 
        /// <see cref="HttpContext"/> instance representing the HTTP 
        /// context during the exception.
        /// </summary>
        public Error(Exception e, HttpContextBase context)
        {
            if (e == null)
                throw new ArgumentNullException("e");

            _exception = e;
            Exception baseException = e.GetBaseException();

            //
            // Load the basic information.
            //

            _hostName = Environment.TryGetMachineName(context);
            _typeName = baseException.GetType().FullName;
            _message = baseException.Message;
            _source = baseException.Source;
            _detail = e.ToString();
            _user = Thread.CurrentPrincipal.Identity.Name ?? string.Empty;
            _time = DateTime.Now;

            //
            // If this is an HTTP exception, then get the status code
            // and detailed HTML message provided by the host.
            //

            HttpException httpException = e as HttpException;

            if (httpException != null)
            {
                _statusCode = httpException.GetHttpCode();
                _webHostHtmlMessage = TryGetHtmlErrorMessage(httpException) ?? string.Empty;
            }

            //
            // If the HTTP context is available, then capture the
            // collections that represent the state request as well as
            // the user.
            //

            if (context != null)
            {
                IPrincipal webUser = context.User;
                if (webUser != null
                    && (webUser.Identity.Name ?? string.Empty).Length > 0)
                {
                    _user = webUser.Identity.Name;
                }

                var request = context.Request;
                var qsfc = request.TryGetUnvalidatedCollections((form, qs, cookies) => new
                {
                    QueryString = qs,
                    Form = form,
                    Cookies = cookies,
                });

                _serverVariables = CopyCollection(request.ServerVariables);

                // patch to include raw URL so error reports are useful when URL rewriting (isite/dvanderwilt)
                _serverVariables.Add("RAW_URL", context.Request.RawUrl);

                _queryString = CopyCollection(qsfc.QueryString);
                _form = CopyCollection(qsfc.Form);
                _cookies = CopyCollection(qsfc.Cookies);
            }

            var callerInfo = e.TryGetCallerInfo() ?? CallerInfo.Empty;
            if (!callerInfo.IsEmpty)
            {
                _detail = "# caller: " + callerInfo
                        + System.Environment.NewLine
                        + _detail;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Error"/> class
        /// from a given <see cref="Exception"/> instance and 
        /// <see cref="HttpContext"/> instance representing the HTTP 
        /// context during the exception.
        /// </summary>
        public Error(Exception e, HttpContextBase context)
        {
            if (e == null)
                throw new ArgumentNullException("e");

            _exception = e;
            Exception baseException = e.GetBaseException();

            //
            // Load the basic information.
            //

            _hostName = Environment.TryGetMachineName(context);
            _typeName = baseException.GetType().FullName;
            _message = baseException.Message;
            _source = baseException.Source;
            _detail = e.ToString();
            _user = Thread.CurrentPrincipal.Identity.Name ?? string.Empty;
            _time = DateTime.Now;

            //
            // If this is an HTTP exception, then get the status code
            // and detailed HTML message provided by the host.
            //

            HttpException httpException = e as HttpException;

            if (httpException != null)
            {
                _statusCode = httpException.GetHttpCode();
                _webHostHtmlMessage = TryGetHtmlErrorMessage(httpException) ?? string.Empty;
            }

            //
            // If the HTTP context is available, then capture the
            // collections that represent the state request as well as
            // the user.
            //

            if (context != null)
            {
                IPrincipal webUser = context.User;
                if (webUser != null
                    && (webUser.Identity.Name ?? string.Empty).Length > 0)
                {
                    _user = webUser.Identity.Name;
                }

                var request = context.Request;
                var qsfc = request.TryGetUnvalidatedCollections((form, qs, cookies) => new
                {
                    QueryString = qs,
                    Form = form,
                    Cookies = cookies,
                });

                _serverVariables = CopyCollection(request.ServerVariables);

                if (_serverVariables != null)
                {
                    // Hack for issue #140:
                    // http://code.google.com/p/elmah/issues/detail?id=140

                    const string authPasswordKey = "AUTH_PASSWORD";
                    string authPassword = _serverVariables[authPasswordKey];
                    if (authPassword != null) // yes, mask empty too!
                        _serverVariables[authPasswordKey] = "*****";
                }

                _queryString = CopyCollection(qsfc.QueryString);
                _form = CopyCollection(qsfc.Form);
                _cookies = CopyCollection(qsfc.Cookies);

                // Adding total bytes of request to output, sometimes this doesn't match
                // the bytes sent in the header
                _serverVariables.Add("TOTAL_BYTES", request.TotalBytes.ToString());

                // re-read the raw input from the request and put it into the output
                try
                {
                    System.IO.Stream inputStream = request.InputStream;
                    inputStream.Position = 0;
                    using (System.IO.StreamReader reader = new System.IO.StreamReader(inputStream))
                    {
                        _serverVariables.Add("RAW_INPUT_STREAM", reader.ReadToEnd());
                    }
                }
                catch (Exception) { }
            }

            var callerInfo = e.TryGetCallerInfo() ?? CallerInfo.Empty;
            if (!callerInfo.IsEmpty)
            {
                _detail = "# caller: " + callerInfo
                        + System.Environment.NewLine
                        + _detail;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Error"/> class
        /// from a given <see cref="Exception"/> instance and 
        /// <see cref="HttpContext"/> instance representing the HTTP 
        /// context during the exception.
        /// </summary>

        public Error(Exception e, HttpContextBase context)
        {
            if (e == null)
                throw new ArgumentNullException("e");

            _exception = e;
            var baseException = e.GetBaseException();

            //
            // Load the basic information.
            //

            try
            {
                _hostName = Environment.MachineName;
            }
            catch (SecurityException)
            {
                // A SecurityException may occur in certain, possibly 
                // user-modified, Medium trust environments.
                _hostName = string.Empty;
            }

            _typeName = baseException.GetType().FullName;
            _message = baseException.Message;
            _source = baseException.Source;
            _detail = e.ToString();
            _user = Thread.CurrentPrincipal.Identity.Name ?? string.Empty;
            _time = DateTime.Now;

            //
            // If this is an HTTP exception, then get the status code
            // and detailed HTML message provided by the host.
            //

            var httpException = e as HttpException;

            if (httpException != null)
            {
                _statusCode = httpException.GetHttpCode();
                _webHostHtmlMessage = TryGetHtmlErrorMessage(httpException) ?? string.Empty;
            }

            //
            // If the HTTP context is available, then capture the
            // collections that represent the state request as well as
            // the user.
            //

            if (context != null)
            {
                var webUser = context.User;
                if (webUser != null 
                    && (webUser.Identity.Name ?? string.Empty).Length > 0)
                {
                    _user = webUser.Identity.Name;
                }

                var request = context.Request;
                var qsfc = request.TryGetUnvalidatedCollections((form, qs, cookies) => new
                {
                    QueryString = qs,
                    Form = form, 
                    Cookies = cookies,
                });

                _serverVariables = CopyCollection(request.ServerVariables);
                _queryString = CopyCollection(qsfc.QueryString);
                _form = CopyCollection(qsfc.Form);
                _cookies = CopyCollection(qsfc.Cookies);
            }

            var callerInfo = e.TryGetCallerInfo() ?? CallerInfo.Empty;
            if (!callerInfo.IsEmpty)
            {
                _detail = "# caller: " + callerInfo
                        + System.Environment.NewLine
                        + _detail;
            }
        }