public static async Task ProcessRequest(HttpContext context, ElmahCore.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())); }
public static Task ProcessRequestAsync(ElmahCore.ErrorLog errorLog, HttpContext context) { if (context == null) { throw new ArgumentNullException("context"); } return(ProcessRequestPrelude(context, (format, maxDownloadCount) => ProcessRequestAsync(errorLog, context, format, maxDownloadCount))); }
async Task ProcessTemplate <T>(HttpContext context, ElmahCore.ErrorLog error) where T : WebTemplateBase, new() { var template = new T { Context = context, ErrorLog = error, ElmahRoot = _elmahRoot }; await context.Response.WriteAsync(template.TransformText()); }
private static IEnumerable <XElement> GetItems(ElmahCore.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 ErrorLogMiddleware(ElmahCore.ErrorLog errorLog, IOptions <ElmahOptions> elmahOptions, ILogger <ErrorLogMiddleware> logger) { _errorLog = errorLog ?? throw new ArgumentNullException(nameof(errorLog)); var lf = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = lf; //_logger = lf.CreateLogger<ErrorLogMiddleware>(); _сheckPermissionAction = elmahOptions?.Value?.CheckPermissionAction; //Notifiers if (elmahOptions?.Value?.Notifiers != null) { _notifiers = elmahOptions.Value.Notifiers.ToList(); } //Filters if (elmahOptions?.Value?.Filters != null) { _filters = elmahOptions.Value.Filters.ToList(); foreach (var errorFilter in _filters) { Filtering += errorFilter.OnErrorModuleFiltering; } } if (!string.IsNullOrEmpty(elmahOptions?.Value?.FiltersConfig)) { try { ConfigureFilters(elmahOptions.Value.FiltersConfig); } catch (Exception e) { _logger.LogError("Error in filters XML file", e); } } if (!string.IsNullOrEmpty(elmahOptions?.Value?.Path)) { _elmahRoot = elmahOptions.Value.Path.ToLower(); } if (!_elmahRoot.StartsWith('/')) { _elmahRoot = "/" + _elmahRoot; } if (_elmahRoot.EndsWith('/')) { _elmahRoot = _elmahRoot.Substring(0, _elmahRoot.Length - 1); } }
public static IEnumerable <AsyncResultOr <string> > ProcessRequest(ElmahCore.ErrorLog errorLog, HttpContext context, Func <AsyncCallback> getAsyncCallback) { if (context == null) { throw new ArgumentNullException("context"); } if (getAsyncCallback == null) { throw new ArgumentNullException("getAsyncCallback"); } return(ProcessRequestPrelude(context, (format, maxDownloadCount) => ProcessRequest(errorLog, context, getAsyncCallback, format, maxDownloadCount))); }
public static void ProcessRequest(HttpContext context, ElmahCore.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(); }
public static async Task ProcessRequest(HttpContext context, ElmahCore.ErrorLog errorLog, string elmahRoot) { var log = errorLog; 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())); }
private static IEnumerable <T> GetErrors <T>(ElmahCore.ErrorLog log, int pageSize, Func <int, ErrorLogEntry, T> resultor) { Debug.Assert(log != null); Debug.Assert(pageSize > 0); Debug.Assert(resultor != null); var entries = new List <ErrorLogEntry>(pageSize); for (var pageIndex = 0; ; pageIndex++) { log.GetErrors(pageIndex, pageSize, entries); if (!entries.Any()) { break; } foreach (var entry in entries) { yield return(resultor(pageIndex, entry)); } entries.Clear(); } }
public static void ProcessRequest(HttpContext context, ElmahCore.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); }
private static IEnumerable <AsyncResultOr <string> > ProcessRequest(ElmahCore.ErrorLog log, HttpContext context, Func <AsyncCallback> getAsyncCallback, Format format, int maxDownloadCount) { var response = context.Response; foreach (var text in format.Header()) { yield return(AsyncResultOr.Value(text)); } var lastBeatTime = DateTime.Now; var errorEntryList = new List <ErrorLogEntry>(PageSize); var downloadCount = 0; for (var pageIndex = 0; ; pageIndex++) { var ar = log.BeginGetErrors(pageIndex, PageSize, errorEntryList, getAsyncCallback(), null); yield return(AsyncResultOr.InsteadOf <string>(ar)); var total = log.EndGetErrors(ar); var count = errorEntryList.Count; if (maxDownloadCount > 0) { var remaining = maxDownloadCount - (downloadCount + count); if (remaining < 0) { count += remaining; } } foreach (var entry in format.Entries(errorEntryList, 0, count, total)) { yield return(AsyncResultOr.Value(entry)); } downloadCount += count; response.Body.Flush(); // // Done if either the end of the list (no more errors found) or // the requested limit has been reached. // if (count == 0 || downloadCount == maxDownloadCount) { if (count > 0) { foreach (var entry in format.Entries(new ErrorLogEntry[0], total)) // Terminator { yield return(AsyncResultOr.Value(entry)); } } break; } // // Poll whether the client is still connected so data is not // unnecessarily sent to an abandoned connection. This check is // only performed at certain intervals. // if (DateTime.Now - lastBeatTime > BeatPollInterval) { lastBeatTime = DateTime.Now; } // // Fetch next page of results. // errorEntryList.Clear(); } }
private static async Task ProcessRequestAsync(ElmahCore.ErrorLog log, HttpContext context, Format format, int maxDownloadCount) { var response = context.Response; var output = response; foreach (var text in format.Header()) { await output.WriteAsync(text); } var errorEntryList = new List <ErrorLogEntry>(PageSize); var downloadCount = 0; for (var pageIndex = 0; ; pageIndex++) { var total = await log.GetErrorsAsync(pageIndex, PageSize, errorEntryList); var count = errorEntryList.Count; if (maxDownloadCount > 0) { var remaining = maxDownloadCount - (downloadCount + count); if (remaining < 0) { count += remaining; } } foreach (var entry in format.Entries(errorEntryList, 0, count, total)) { await response.WriteAsync(entry); } downloadCount += count; await response.Body.FlushAsync(); // // Done if either the end of the list (no more errors found) or // the requested limit has been reached. // if (count == 0 || downloadCount == maxDownloadCount) { if (count > 0) { foreach (var entry in format.Entries(new ErrorLogEntry[0], total)) // Terminator { await response.WriteAsync(entry); } } break; } // // Fetch next page of results. // errorEntryList.Clear(); } }