private static async Task <string> GetContentAsync(ContentStream stream, IDictionary <string, string[]> headers, long maxLength) { const string contentType = "Content-Type"; string[] value; if (headers.TryGetValue(contentType, out value)) { if (value != null && value.Length > 0) { return(await stream.ReadContentAsync(value[0], maxLength)); } } return(null); }
public override async Task Invoke(IOwinContext context) { ContentStream requestStream = new ContentStream(new MemoryStream(), context.Request.Body); context.Request.Body = requestStream; ContentStream responseStream = new ContentStream(new MemoryStream(), context.Response.Body); context.Response.Body = responseStream; HttpEntry entry = new HttpEntry(); Action <object> callback = o => { IOwinContext ctx = (IOwinContext)o; ctx.Response.Headers.Add(_trackingHeaderName, new[] { entry.RequestId.ToString() }); }; context.Response.OnSendingHeaders(callback, context); await Next.Invoke(context); entry.Verb = context.Request.Method; entry.RequestUri = context.Request.Uri; entry.RequestHeaders = context.Request.Headers; entry.RequestLength = requestStream.ContentLength; entry.Request = await GetContentAsync(requestStream, context.Request.Headers, _maxRequestLength); entry.StatusCode = context.Response.StatusCode; entry.ReasonPhrase = context.Response.ReasonPhrase; entry.ResponseHeaders = context.Response.Headers; entry.ResponseLength = responseStream.ContentLength; entry.Response = await GetContentAsync(responseStream, context.Response.Headers, _maxResponseLength); if (_whatToDo != null) { _whatToDo(entry); } }