public static async Task ProcessRequest(HttpContext context, ErrorLog errorLog, string elmahRoot) { const int pageSize = 15; var entries = new List <ErrorLogEntry>(pageSize); var log = errorLog; log.GetErrors(0, pageSize, entries); var response = context.Response; response.ContentType = "application/xml"; var title = string.Format(@"Error log of {0} on {1}", log.ApplicationName, 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, "Log 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 = string.Format("Digest for {0} ({1})", runningDay.ToString("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)); } }
public static async Task ProcessRequest(HttpContext context, ErrorLog errorLog, string elmahRoot) { var log = errorLog; var request = context.Request; var response = context.Response; response.ContentType = "application/xml"; var title = string.Format(@"Daily digest of errors in {0} on {1}", log.ApplicationName, Environment.MachineName); var link = $"{context.Request.Scheme}://{context.Request.Host}{elmahRoot}"; var baseUrl = new Uri(link.TrimEnd('/') + "/"); var items = GetItems(log, baseUrl, 30, 30).Take(30); var rss = RssXml.Rss(title, link, "Daily digest of application errors", items); await context.Response.WriteAsync(XmlText.StripIllegalXmlCharacters(rss.ToString())); }