Stream LogResponse(Guid guid, HttpResponseMessage httpResponse, HttpContentHeaders responseBodyHeaders, Stream responseBodyStream, TimeSpan duration)
        {
            var logger = _communicatorLogger;

            if (logger == null)
            {
                return(responseBodyStream);
            }

            var sb = new ResponseLogMessageBuilder(guid.ToString(), httpResponse.StatusCode)
            {
                Duration = duration
            };

            foreach (var header in httpResponse.Headers)
            {
                sb.AddHeader(header.Key, string.Join(" ", header.Value));
            }
            if (responseBodyHeaders != null)
            {
                foreach (var header in responseBodyHeaders)
                {
                    sb.AddHeader(header.Key, string.Join(" ", header.Value));
                }
            }

            var contentType = responseBodyHeaders.ContentType.ToString();

            if (IsBinaryContent(contentType))
            {
                sb.SetBinaryContentBody(contentType);
                logger.Log(sb.Message);
                return(responseBodyStream);
            }

            var    sr           = new StreamReader(responseBodyStream);
            string responseBody = sr.ReadToEnd();

            if (!string.IsNullOrEmpty(responseBody))
            {
                sb.SetBody(responseBody, contentType);
            }
            logger.Log(sb.Message);
            if (responseBodyStream.CanSeek)
            {
                responseBodyStream.Seek(0, SeekOrigin.Begin);
                return(responseBodyStream);
            }
            MemoryStream stream = new MemoryStream();
            StreamWriter writer = new StreamWriter(stream);

            writer.Write(responseBody);
            writer.Flush();
            stream.Position = 0;
            return(stream);
        }
Ejemplo n.º 2
0
        void LogResponse(Guid guid, Uri uri, HttpResponseMessage httpResponse, HttpContentHeaders responseBodyHeaders, string responseBody, TimeSpan duration)
        {
            var sb = new ResponseLogMessageBuilder(guid.ToString(), httpResponse.StatusCode);

            sb.Duration = duration;
            foreach (var header in httpResponse.Headers)
            {
                sb.AddHeader(header.Key, string.Join(" ", header.Value));
            }
            if (responseBodyHeaders != null)
            {
                foreach (var header in responseBodyHeaders)
                {
                    sb.AddHeader(header.Key, string.Join(" ", header.Value));
                }
            }
            if (responseBody != "")
            {
                sb.SetBody(responseBody, responseBodyHeaders.ContentType.ToString());
            }
            _communicatorLogger?.Log(sb.Message);
        }