Exemplo n.º 1
0
        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();
            }
        }