private void LogRequestLoggingInfo(HttpRequestMessage request) { var info = new ApiLoggingInfo(); info.HttpMethod = request.Method.Method; info.UriAccessed = request.RequestUri.AbsoluteUri; info.IpAddress = HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "0.0.0.0"; info.MessageType = HttpMessageType.Request; ExtractMessageHeadersIntoLoggingInfo(info, request.Headers.ToList()); if (request.Content != null) { request.Content.ReadAsByteArrayAsync() .ContinueWith(task => { info.BodyContent = Encoding.UTF8.GetString(task.Result); _repository.Log(info); }); return; } _repository.Log(info); }
private void ExtractMessageHeadersIntoLoggingInfo(ApiLoggingInfo info, List<KeyValuePair<string, IEnumerable<string>>> headers) { headers.ForEach(h => { // convert the header values into one long string from a series of IEnumerable<string> values so it looks for like a HTTP header var headerValues = new StringBuilder(); if (h.Value != null) { foreach (var hv in h.Value) { if (headerValues.Length > 0) { headerValues.Append(", "); } headerValues.Append(hv); } } info.Headers.Add(string.Format("{0}: {1}", h.Key, headerValues.ToString())); }); }