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

            response.ContentType = "application/json";

            //
            // 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 JSON.
            //

            using (var sw = new StreamWriter(response.Body))
                ErrorJson.Encode(entry.Error, sw);
        }
            public override IEnumerable <string> Entries(IList <ErrorLogEntry> entries, int index, int count, int total)
            {
                Debug.Assert(entries != null);
                Debug.Assert(index >= 0);
                Debug.Assert(index + count <= entries.Count);

                var writer = new StringWriter {
                    NewLine = "\n"
                };

                if (_wrapped)
                {
                    writer.WriteLine("<script type='text/javascript' language='javascript'>");
                    writer.WriteLine("//<[!CDATA[");
                }

                writer.Write(_callback);
                writer.Write('(');

                var json = new JsonTextWriter(writer);

                json.Object()
                .Member("total").Number(total)
                .Member("errors").Array();

                var requestUrl = $"{Context.Request.Scheme}://{Context.Request.Host}{Context.Request.Path}";

                for (var i = index; i < count; i++)
                {
                    var entry = entries[i];
                    writer.WriteLine();
                    if (i == 0)
                    {
                        writer.Write(' ');
                    }
                    writer.Write("  ");

                    var urlTemplate = $"{requestUrl}?id=" + Uri.EscapeDataString(entry.Id);

                    json.Object();
                    ErrorJson.EncodeMembers(entry.Error, json);
                    json.Member("hrefs")
                    .Array()
                    .Object()
                    .Member("type").String("text/html")
                    .Member("href").String(string.Format(urlTemplate, "detail")).Pop()
                    .Object()
                    .Member("type").String("aplication/json")
                    .Member("href").String(string.Format(urlTemplate, "json")).Pop()
                    .Object()
                    .Member("type").String("application/xml")
                    .Member("href").String(string.Format(urlTemplate, "xml")).Pop()
                    .Pop()
                    .Pop();
                }

                json.Pop();
                json.Pop();

                if (count > 0)
                {
                    writer.WriteLine();
                }

                writer.WriteLine(");");

                if (_wrapped)
                {
                    writer.WriteLine("//]]>");
                    writer.WriteLine("</script>");

                    if (count == 0)
                    {
                        writer.WriteLine(@"</body></html>");
                    }
                }

                yield return(writer.ToString());
            }