public static async Task ProcessRequest(HttpContext context, ErrorLog errorLog, string elmahRoot) { const int pageSize = 15; var entries = new List <ErrorLogEntry>(pageSize); var log = errorLog; await log.GetErrorsAsync(0, pageSize, entries); var response = context.Response; response.ContentType = "application/xml"; var title = $@"Error log of {log.ApplicationName} on {Environment.MachineName}"; var link = $"{context.Request.Scheme}://{context.Request.Host}{elmahRoot}"; var baseUrl = new Uri(link.TrimEnd('/') + "/"); var items = from entry in entries let error = entry.Error select RssXml.Item( error.Message, "An error of type " + error.Type + " occurred. " + error.Message, error.Time, baseUrl + "detail?id=" + Uri.EscapeDataString(entry.Id)); var rss = RssXml.Rss(title, link, "AddMessage of recent errors", items); await response.WriteAsync(XmlText.StripIllegalXmlCharacters(rss.ToString())); }
private static IEnumerable <XElement> GetItems(ErrorLog log, Uri baseUrl, int pageSize, int maxPageLimit) { Debug.Assert(log != null); Debug.Assert(baseUrl != null); Debug.Assert(baseUrl.IsAbsoluteUri); Debug.Assert(pageSize > 0); var runningDay = DateTime.MaxValue; var runningErrorCount = 0; string title = null; DateTime?pubDate = null; var sb = new StringBuilder(); var writer = new StringWriter(sb); var source = GetErrors(log, pageSize, (p, e) => new { PageIndex = p, Entry = e }); foreach (var entry in from item in source.TakeWhile(e => e.PageIndex < maxPageLimit) select item.Entry) { var error = entry.Error; var time = error.Time.ToUniversalTime(); var day = time.Date; // // If we're dealing with a new day then break out to a // new channel item, finishing off the previous one. // if (day < runningDay) { if (runningErrorCount > 0) { RenderEnd(writer); Debug.Assert(title != null); Debug.Assert(pubDate != null); yield return(RssXml.Item(title, sb.ToString(), pubDate.Value)); } runningDay = day; runningErrorCount = 0; pubDate = time; title = $"Digest for {runningDay:yyyy-MM-dd} ({runningDay.ToLongDateString()})"; sb.Length = 0; RenderStart(writer); } RenderError(writer, entry, baseUrl); runningErrorCount++; } if (runningErrorCount > 0) { RenderEnd(writer); Debug.Assert(title != null); Debug.Assert(pubDate != null); yield return(RssXml.Item(title, sb.ToString(), pubDate.Value)); } }