예제 #1
0
        /// <summary>
        /// Reads the error data in XML attributes.
        /// </summary>

        protected virtual void ReadXmlAttributes(XmlReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (!reader.IsStartElement())
            {
                throw new ArgumentOutOfRangeException("reader");
            }

            _applicationName = reader.GetAttribute("application");
            _hostName        = reader.GetAttribute("host");
            _typeName        = reader.GetAttribute("type");
            _message         = reader.GetAttribute("message");
            _source          = reader.GetAttribute("source");
            _detail          = reader.GetAttribute("detail");
            _user            = reader.GetAttribute("user");
            string timeString = StringEtc.MaskNull(reader.GetAttribute("time"));

            _time = timeString.Length == 0 ? new DateTime() : XmlConvert.ToDateTime(timeString, System.Xml.XmlDateTimeSerializationMode.Local);
            string statusCodeString = StringEtc.MaskNull(reader.GetAttribute("statusCode"));

            _statusCode         = statusCodeString.Length == 0 ? 0 : XmlConvert.ToInt32(statusCodeString);
            _webHostHtmlMessage = reader.GetAttribute("webHostHtmlMessage");
        }
예제 #2
0
        protected override void OnLoad(EventArgs e)
        {
            //
            // Retrieve the ID of the error to display and read it from
            // the store.
            //

            string errorId = StringEtc.MaskNull(this.Request.QueryString["id"]);

            if (errorId.Length == 0)
            {
                return;
            }

            _errorEntry = this.ErrorLog.GetError(errorId);

            //
            // Perhaps the error has been deleted from the store? Whatever
            // the reason, bail out silently.
            //

            if (_errorEntry == null)
            {
                return;
            }

            //
            // Setup the title of the page.
            //

            this.Title = string.Format("Error: {0} [{1}]", _errorEntry.Error.Type, _errorEntry.Id);

            base.OnLoad(e);
        }
예제 #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, HttpContext context)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

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

            //
            // Load the basic information.
            //

            _hostName = Environment.MachineName;
            _typeName = baseException.GetType().FullName;
            _message  = baseException.Message;
            _source   = baseException.Source;
            _detail   = e.ToString();
            _user     = StringEtc.MaskNull(Thread.CurrentPrincipal.Identity.Name);
            _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 = StringEtc.MaskNull(httpException.GetHtmlErrorMessage());
            }

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

            if (context != null)
            {
                HttpRequest request = context.Request;

                _serverVariables = CopyCollection(request.ServerVariables);
                _queryString     = CopyCollection(request.QueryString);
                _form            = CopyCollection(request.Form);
                _cookies         = CopyCollection(request.Cookies);
            }
        }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SqlErrorLog"/> class
        /// using a dictionary of configured settings.
        /// </summary>

        public SqlErrorLog(IDictionary config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            //
            // Get the connection string. If it is empty, then check for
            // another setting called connectionStringAppKey. The latter
            // specifies the key in appSettings that contains the actual
            // connection string to be used.
            //

            string connectionStringName = StringEtc.MaskNull((string)config[CONNECTION_STRING_NAME]);

            if (connectionStringName.Length == 0)
            {
                string connectionStringAppKey = StringEtc.MaskNull((string)config["connectionStringAppKey"]);

                if (connectionStringAppKey.Length != 0)
                {
                    _connectionString = ConfigurationManager.AppSettings[connectionStringAppKey];
                }
            }
            else
            {
                if (ConfigurationManager.ConnectionStrings[connectionStringName] == null)
                {
                    throw new ApplicationException(string.Format("Connection string name '{0}' not found in ConfigurationManager.ConnectionStrings", connectionStringName));
                }
                _connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
            }

            //
            // If there is no connection string to use then throw an
            // exception to abort construction.
            //

            if (_connectionString.Length == 0)
            {
                throw new ApplicationException("Connection string is missing for the SQL error log.");
            }
        }
예제 #5
0
        public static object CreateFromConfigSection(string sectionName)
        {
            Debug.AssertStringNotEmpty(sectionName);

            //
            // Get the configuration section with the settings.
            //

            IDictionary config = (IDictionary)ConfigurationManager.GetSection(sectionName);

            if (config == null)
            {
                return(null);
            }

            //
            // We modify the settings by removing items as we consume
            // them so make a copy here.
            //

            config = (IDictionary)((ICloneable)config).Clone();

            //
            // Get the type specification of the service provider.
            //

            string typeSpec = StringEtc.MaskNull((string)config["type"]);

            if (typeSpec.Length == 0)
            {
                return(null);
            }

            config.Remove("type");

            //
            // Locate, create and return the service provider object.
            //

            Type type = Type.GetType(typeSpec, true);

            return(Activator.CreateInstance(type, new object[] { config }));
        }
예제 #6
0
        private static string GetSetting(IDictionary config, string name, string defaultValue)
        {
            Debug.Assert(config != null);
            Debug.AssertStringNotEmpty(name);

            string value = StringEtc.MaskNull((string)config[name]);

            if (value.Length == 0)
            {
                if (defaultValue == null)
                {
                    throw new ApplicationException(string.Format(
                                                       "The required configuration setting '{0}' is missing for the error mailing module.", name));
                }

                value = defaultValue;
            }

            return(value);
        }
예제 #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MemoryErrorLog"/> class
        /// using a dictionary of configured settings.
        /// </summary>

        public MemoryErrorLog(IDictionary config)
        {
            if (config == null)
            {
                _size = DefaultSize;
            }
            else
            {
                string sizeString = StringEtc.MaskNull((string)config["size"]);

                if (sizeString.Length == 0)
                {
                    _size = DefaultSize;
                }
                else
                {
                    _size = Convert.ToInt32(sizeString, CultureInfo.InvariantCulture);
                    _size = Math.Max(0, Math.Min(MaximumSize, _size));
                }
            }
        }
예제 #8
0
        protected override void Render(HtmlTextWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            //
            // Retrieve the ID of the error to display and read it from
            // the log.
            //

            string errorId = StringEtc.MaskNull(this.Request.QueryString["id"]);

            if (errorId.Length == 0)
            {
                return;
            }

            ErrorLogEntry errorEntry = this.ErrorLog.GetError(errorId);

            if (errorEntry == null)
            {
                return;
            }

            //
            // If we have a host (ASP.NET) formatted HTML message
            // for the error then just stream it out as our response.
            //

            if (errorEntry.Error.WebHostHtmlMessage.Length != 0)
            {
                writer.Write(errorEntry.Error.WebHostHtmlMessage);
            }
        }
예제 #9
0
        /// <summary>
        /// Renders a collection as a table in HTML document body.
        /// </summary>
        /// <remarks>
        /// This method is called by <see cref="RenderCollections"/> to
        /// format a diagnostic collection from <see cref="Error"/> object.
        /// </remarks>

        protected virtual void RenderCollection(NameValueCollection collection, string caption)
        {
            if (collection == null || collection.Count == 0)
            {
                return;
            }

            HtmlTextWriter writer = this.Writer;

            writer.RenderBeginTag(HtmlTextWriterTag.H1);
            HttpUtility.HtmlEncode(caption, writer);
            writer.RenderEndTag(); // </h1>
            writer.WriteLine();

            //
            // Write a table with each key in the left column
            // and its value in the right column.
            //

            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "5");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Border, "1");
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            //
            // Write the column headings.
            //

            writer.AddAttribute(HtmlTextWriterAttribute.Valign, "top");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            writer.AddAttribute(HtmlTextWriterAttribute.Align, "left");
            writer.RenderBeginTag(HtmlTextWriterTag.Th);
            writer.Write("Name");
            writer.RenderEndTag(); // </th>

            writer.AddAttribute(HtmlTextWriterAttribute.Align, "left");
            writer.RenderBeginTag(HtmlTextWriterTag.Th);
            writer.Write("Value");
            writer.RenderEndTag(); // </th>

            writer.RenderEndTag(); // </tr>

            //
            // Write the main body of the table containing keys
            // and values in a two-column layout.
            //

            foreach (string key in collection.Keys)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Valign, "top");
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                HttpUtility.HtmlEncode(key, writer);
                writer.RenderEndTag(); // </td>

                writer.RenderBeginTag(HtmlTextWriterTag.Td);

                string value = StringEtc.MaskNull(collection[key]);

                if (value.Length != 0)
                {
                    HttpUtility.HtmlEncode(value, writer);
                }
                else
                {
                    writer.Write("&nbsp;");
                }

                writer.RenderEndTag(); // </td>

                writer.RenderEndTag(); // </tr>
            }

            writer.RenderEndTag(); // </table>
            writer.WriteLine();
        }
예제 #10
0
 public static void AssertStringNotEmpty(string s)
 {
     BaseDebug.Assert(StringEtc.MaskNull(s).Length != 0);
 }
예제 #11
0
        /// <summary>
        /// Schedules the error to be e-mailed synchronously.
        /// </summary>

        protected virtual void ReportError(Error error)
        {
            if (error == null)
            {
                throw new ArgumentNullException("error");
            }

            //
            // Start by checking if we have a sender and a recipient.
            // These values may be null if someone overrides the
            // implementation of Init but does not override the
            // MailSender and MailRecipient properties.
            //

            string sender    = StringEtc.MaskNull(this.MailSender);
            string recipient = StringEtc.MaskNull(this.MailRecipient);

            if (sender.Length == 0 || recipient.Length == 0)
            {
                return;
            }

            //
            // Create the mail, setting up the sender and recipient.
            //

            MailMessage mail = new MailMessage();

            mail.From = new MailAddress(sender);
            mail.To.Add(new MailAddress(recipient));

            //
            // Format the mail subject.
            //

            string subjectFormat = StringEtc.MaskNull(this.MailSubjectFormat);

            if (subjectFormat.Length == 0)
            {
                subjectFormat = "Error ({1}): {0}";
            }

            mail.Subject = string.Format(subjectFormat, error.Message, error.Type);

            //
            // Format the mail body.
            //

            ErrorTextFormatter formatter = CreateErrorFormatter();

            StringWriter bodyWriter = new StringWriter();

            formatter.Format(bodyWriter, error);
            mail.Body = bodyWriter.ToString();

            switch (formatter.MimeType)
            {
            case "text/html": mail.IsBodyHtml = true; break;

            case "text/plain": mail.IsBodyHtml = false;  break;

            default:
            {
                throw new ApplicationException(string.Format(
                                                   "The error mail module does not know how to handle the {1} media type that is created by the {0} formatter.",
                                                   formatter.GetType().FullName, formatter.MimeType));
            }
            }

            //
            // Provide one last hook to pre-process the mail and then send
            // it off.
            //

            try
            {
                PreSendMail(mail, error);
                SendMail(mail);
            }
            finally
            {
                DisposeMail(mail);
            }
        }