private void ProcessRequest(Task <string> task, ApiLogEntry apiLogEntry) { if (!_helper.ShouldLog) { return; } apiLogEntry.RequestContentBody = task.Result; if (string.IsNullOrEmpty(apiLogEntry.RequestContentBody)) { apiLogEntry.RequestContentBody = "No body payload detected"; } _traceStepper.WriteOperation("Web API request", "request headers", apiLogEntry.RequestHeaders); _traceStepper.WriteOperation("Web API request", "query string", apiLogEntry.RequestUri); _traceStepper.WriteOperation("Web API request", "body request", apiLogEntry.RequestContentBody); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { ApiLogEntry apiLogEntry = null; if (_helper.ShouldLog) { apiLogEntry = CreateApiLogEntryWithRequestData(request); } if (request.Content != null) { await request.Content.ReadAsStringAsync() .ContinueWith(task => { ProcessRequest(task, apiLogEntry); }, cancellationToken); } return(await base.SendAsync(request, cancellationToken).ContinueWith(task => AsyncHelper.RunSync(() => ProcessResponse(task, apiLogEntry)), cancellationToken)); }
private async Task <HttpResponseMessage> ProcessResponse(Task <HttpResponseMessage> task, ApiLogEntry apiLogEntry) { var response = task.Result; if (!_helper.ShouldLog) { return(response); } // Update the API log entry with response info apiLogEntry.ResponseStatusCode = (int)response.StatusCode; apiLogEntry.ResponseTimestamp = DateTime.Now; if (response.Content != null) { apiLogEntry.ResponseContentBody = response.Content.ReadAsStringAsync().Result; apiLogEntry.ResponseContentType = response.Content.Headers.ContentType.MediaType; apiLogEntry.ResponseHeaders = SerializeHeaders(response.Content.Headers); } // TODO: Save the API log entry to the database _traceStepper.WriteOperation("Web API response", "response body", apiLogEntry.ResponseContentBody); _traceStepper.WriteOperation("Web API response", "response headers", apiLogEntry.ResponseHeaders); _traceStepper.Dispose(); var traceSteps = _tracer.TraceSteps; _tracer.Clear(); await _bus.SendAsync(new ApiEntryCommand(apiLogEntry, traceSteps)); return(response); }