Exemplo n.º 1
0
        private void LogResponseLoggingInfo(HttpResponseMessage response)
        {
            var info = new ApiLoggingInfo();
            info.MessageType = HttpMessageType.Response;
            info.HttpMethod = response.RequestMessage.Method.ToString();
            info.ResponseStatusCode = response.StatusCode;
            info.ResponseStatusMessage = response.ReasonPhrase;
            info.UriAccessed = response.RequestMessage.RequestUri.AbsoluteUri;
            info.IpAddress = HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "0.0.0.0";

            ExtractMessageHeadersIntoLoggingInfo(info, response.Headers.ToList());
            
            if (response.Content != null)
            {
                response.Content.ReadAsByteArrayAsync()
                    .ContinueWith(task =>
                    {
                        var responseMsg = Encoding.UTF8.GetString(task.Result);
                        info.BodyContent = responseMsg;
                        _repository.Log(info);
                    });

                return;
            }

            _repository.Log(info);
        }
		public void Log(ApiLoggingInfo loggingInfo)
		{
			LogMessageCount++;

			if (loggingInfo.MessageType == HttpMessageType.Response)
			{
				HasResponseMessageTypeBeenReceived = true;
			}
			else
			{
				HasRequestMessageTypeBeenReceived = true;
			}

			Trace.WriteLine(string.Format("Message: Type:{0}, Uri:{1}, Method:{2}", loggingInfo.MessageType, loggingInfo.UriAccessed, loggingInfo.HttpMethod));
		}
Exemplo n.º 3
0
        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()));
            });
        }