public static async Task <TResponse> ExecuteAsync <TResponse>(this HttpClient http, HttpRequestBase <TResponse> httpRequest, CancellationToken cancellationToken) where TResponse : HttpResponse { if (http == null) { throw new ArgumentNullException(nameof(http)); } if (httpRequest == null) { throw new ArgumentNullException(nameof(httpRequest)); } using (var request = new HttpRequestMessage(httpRequest.Method, httpRequest.Resource)) { await httpRequest.OnRequestExecuting(cancellationToken).ConfigureAwait(false); // add all our request headers foreach (var header in httpRequest.Headers) { request.Headers.Add(header.Key, header.Value); } var content = httpRequest.GetContent(); try { request.Content = content; using (var response = await http.SendAsync(request, cancellationToken).ConfigureAwait(false)) { await httpRequest.OnRequestExecuted(cancellationToken).ConfigureAwait(false); return(await httpRequest.GetResponseAsync(response, cancellationToken).ConfigureAwait(false)); } } finally { // dispose our content if we have any content?.Dispose(); } } }
public ILoggingEventContextBuilder IncludeRequestBody() { if (_request == null) { return(this); } Func <string, string> sanitize = s => s.IfNotNull(x => x .SanitizeSensitiveInfo()).IfNotNull(x => x .RemoveViewState()); _props.SafeSetProp("RequestBody", () => { var requestForm = _request.Form.IfNotNull(x => x.ToString()); var content = !string.IsNullOrEmpty(requestForm) ? requestForm : _request.GetContent(); return(sanitize(content)); }); return(this); }