public override async Task Invoke(IOwinContext context) { PathString subPath; context.Request.Path.StartsWithSegments(options.Path, out subPath); if (!subPath.StartsWithSegments(new PathString("/rss"))) { await Next.Invoke(context); return; } const int pageSize = 15; var errorLogEntries = await errorLog.GetErrorsAsync(0, pageSize); var syndicationFeed = new SyndicationFeed(); var hostName = EnvironmentUtilities.GetMachineNameOrDefault("Unknown Host"); syndicationFeed.Title = new TextSyndicationContent($"Error log of {errorLog.ApplicationName} on {hostName}."); syndicationFeed.Description = new TextSyndicationContent("Log of recent errors"); syndicationFeed.Language = "en-us"; var uriAsString = context.Request.Uri.ToString(); var baseUri = new Uri(uriAsString.Remove(uriAsString.LastIndexOf("/rss", StringComparison.InvariantCulture))); syndicationFeed.Links.Add(SyndicationLink.CreateAlternateLink(baseUri)); var items = new List <SyndicationItem>(); foreach (var errorLogEntry in errorLogEntries) { var item = new SyndicationItem { Title = SyndicationContent.CreatePlaintextContent(errorLogEntry.Error.Message), Content = SyndicationContent.CreatePlaintextContent( $"An error of type {errorLogEntry.Error.TypeName} occurred. {errorLogEntry.Error.Message}"), PublishDate = errorLogEntry.Error.Time }; item.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(baseUri, $"/detail?id={errorLogEntry.Id}"))); items.Add(item); } syndicationFeed.Items = items; context.Response.ContentType = "application/rss+xml"; context.Response.StatusCode = 200; using (var writer = XmlWriter.Create(context.Response.Body, SettingsUtility.XmlWriterSettings)) { var formatter = new Rss20FeedFormatter(syndicationFeed); formatter.WriteTo(writer); } }
public override async Task Invoke(IOwinContext context) { PathString subPath; context.Request.Path.StartsWithSegments(options.Path, out subPath); if (!subPath.StartsWithSegments(new PathString("/download"))) { await Next.Invoke(context); return; } const int defaultPageSize = 100; var pageIndex = 0; var count = 0; var total = await errorLog.GetTotalErrorCountAsync(); var limit = Convert.ToInt32(context.Request.Query["limit"]); var maxDownloadCount = limit > 0 ? Math.Min(total, limit) : total; var requestUrl = context.Request.Uri; context.Response.ContentType = "text/csv; header=present"; context.Response.Headers.Set("Content-Disposition", "attachment; filename=errorlog.csv"); context.Response.StatusCode = 200; using (var writer = new StreamWriter(context.Response.Body, Encoding.UTF8)) { await writer.WriteLineAsync("Application,Host,Time,Type,Source,User,Status Code,Message,URL,JSONREF"); do { var pageSize = Math.Min(maxDownloadCount - count, defaultPageSize); var errorLogEntries = await errorLog.GetErrorsAsync(pageIndex ++, pageSize); count += errorLogEntries.Count; foreach (var errorLogEntry in errorLogEntries) { var error = errorLogEntry.Error; var time = error.Time.ToUniversalTime(); var query = $"?id={errorLogEntry.Id}"; await writer.WriteLineAsync($"{error.ApplicationName},{error.HostName},{time.ToString("yyyy-MM-dd HH:mm:ss")},{error.TypeName},{error.Source},{error.User},{error.StatusCode},{error.Message},{new Uri(requestUrl, "detail" + query)},{new Uri(requestUrl, "json" + query)}"); } } while (count < maxDownloadCount); } }
private async Task <IEnumerable <ErrorLogEntry> > GetAllEntriesAsync() { const int defaultPageSize = 30; const int maxPageLimit = 30; var totalErrorCount = await errorLog.GetTotalErrorCountAsync(); var count = 0; var list = new List <ErrorLogEntry>(); for (var pageIndex = 0; pageIndex < maxPageLimit && count < totalErrorCount; pageIndex++) { var entries = await errorLog.GetErrorsAsync(pageIndex, defaultPageSize); count += entries.Count; list.AddRange(entries); } return(list); }