public async System.Threading.Tasks.Task InsertRecordAsync(HttpEntry record) { var path = Path.Combine(path_, record.TrackingId.ToString("d")); using (var stream = File.OpenWrite(path)) using (var writer = new StreamWriter(stream)) await writer.WriteAsync(JsonConvert.SerializeObject(record)); Console.WriteLine("Verb: {0}", record.Verb); Console.WriteLine("RequestUri: {0}", record.RequestUri); Console.WriteLine("Request: {0}", record.Request); Console.WriteLine("RequestLength: {0}", record.RequestLength); Console.WriteLine("StatusCode: {0}", record.StatusCode); Console.WriteLine("ReasonPhrase: {0}", record.ReasonPhrase); Console.WriteLine("Response: {0}", record.Response); Console.WriteLine("Content-Length: {0}", record.ResponseLength); Console.WriteLine("FILE {0} saved.", path); }
private static void WriteResponseHeaders(IOwinResponse response, HttpEntry record) { record.StatusCode = response.StatusCode; record.ReasonPhrase = response.ReasonPhrase; record.ResponseHeaders = response.Headers; }
/// <summary> /// Processes the incoming HTTP call and capture details about /// the request, the response, the identity of the caller and the /// call duration to persistent storage. /// </summary> /// <param name="context">A reference to the Owin context.</param> /// <returns /> public override async Task Invoke(IOwinContext context) { var request = context.Request; var response = context.Response; // capture details about the caller identity var identity = request.User != null && request.User.Identity.IsAuthenticated ? request.User.Identity.Name : "(anonymous)" ; var record = new HttpEntry { CallerIdentity = identity, }; string origin = request.Headers.ContainsKey("Origin") ? request.Headers.Get("Origin") : ""; if (!String.IsNullOrEmpty(origin) && !origin.Contains("null")) record.RequestOrigin = origin; else record.RequestOrigin = "*"; // replace the request stream in order to intercept downstream reads var requestBuffer = new MemoryStream(); var requestStream = new ContentStream(requestBuffer, request.Body); request.Body = requestStream; // replace the response stream in order to intercept downstream writes var responseBuffer = new MemoryStream(); var responseStream = new ContentStream(responseBuffer, response.Body); response.Body = responseStream; // add the "Http-Tracking-Id" response header context.Response.OnSendingHeaders(state => { var ctx = state as IOwinContext; var resp = ctx.Response; // adding the tracking id response header so that the user // of the API can correlate the call back to this entry resp.Headers.Add(trackingIdPropertyName_, new[] { record.TrackingId.ToString("d"), }); resp.Headers.Add(AllowCorsPropertyName_, new[] { record.RequestOrigin, }); }, context) ; // invoke the next middleware in the pipeline await Next.Invoke(context); // rewind the request and response buffers // and record their content WriteRequestHeaders(request, record); record.RequestLength = requestStream.ContentLength; record.Request = await WriteContentAsync(requestStream, record.RequestHeaders, maxRequestLength_); WriteResponseHeaders(response, record); record.ResponseLength = responseStream.ContentLength; record.Response = await WriteContentAsync(responseStream, record.ResponseHeaders, maxResponseLength_); // persist details of the call to durable storage await storage_.InsertRecordAsync(record); }
private static void WriteRequestHeaders(IOwinRequest request, HttpEntry record) { record.Verb = request.Method; record.RequestUri = request.Uri; record.RequestHeaders = request.Headers; }