Example #1
0
        public static void ProcessRequest(HttpContext context, ErrorLog errorLog)
        {
            var response = context.Response;

            response.ContentType = "application/xml";

            //
            // Retrieve the ID of the requested error and read it from
            // the store.
            //

            var errorId = context.Request.Query["id"].FirstOrDefault();

            if (string.IsNullOrEmpty(errorId))
            {
                throw new ApplicationException("Missing error identifier specification.");
            }

            var entry = errorLog.GetError(errorId);

            //
            // Perhaps the error has been deleted from the store? Whatever
            // the reason, pretend it does not exist.
            //

            if (entry == null)
            {
                context.Response.StatusCode = 404;
            }

            //
            // Stream out the error as formatted XML.
            //

            var settings = new XmlWriterSettings();

            settings.Indent = true;
            settings.NewLineOnAttributes = true;
            settings.CheckCharacters     = false;
            var writer = XmlWriter.Create(response.Body, settings);

            writer.WriteStartDocument();
            writer.WriteStartElement("error");
            ErrorXml.Encode(entry.Error, writer);
            writer.WriteEndElement(/* error */);
            writer.WriteEndDocument();
            writer.Flush();
        }
Example #2
0
        /// <summary>
        /// Logs an error to the database.
        /// </summary>
        /// <remarks>
        /// Logs an error as a single XML file stored in a folder. XML files are named with a
        /// sortable date and a unique identifier. Currently the XML files are stored indefinately.
        /// As they are stored as files, they may be managed using standard scheduled jobs.
        /// </remarks>

        public override string Log(Error error)
        {
            var logPath = LogPath;

            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }

            var errorId = Guid.NewGuid().ToString();

            var timeStamp = (error.Time > DateTime.MinValue ? error.Time : DateTime.Now);

            var fileName = string.Format(CultureInfo.InvariantCulture,
                                         @"error-{0:yyyy-MM-ddHHmmssZ}-{1}.xml",
                                         /* 0 */ timeStamp.ToUniversalTime(),
                                         /* 1 */ errorId);

            var path = Path.Combine(logPath, fileName);

            try
            {
                using (var writer = new XmlTextWriter(path, Encoding.UTF8))
                {
                    writer.Formatting = Formatting.Indented;
                    writer.WriteStartElement("error");
                    writer.WriteAttributeString("errorId", errorId);
                    ErrorXml.Encode(error, writer);
                    writer.WriteEndElement();
                    writer.Flush();
                }
            }
            catch (IOException)
            {
                // If an IOException is thrown during writing the file,
                // it means that we will have an either empty or
                // partially written XML file on disk. In both cases,
                // the file won't be valid and would cause an error in
                // the UI.
                File.Delete(path);
                throw;
            }

            return(errorId);
        }