Exemple #1
0
        private void LogRequestLoggingInfo(HttpRequestMessage request)
        {
            var info = new ApiLoggingInfo
            {
                HttpMethod   = request.Method.Method,
                UriAccessed  = request.RequestUri.AbsoluteUri,
                IpAddress    = request.GetClientIpAddress(),
                MessageType  = HttpMessageType.Request,
                LoggingLevel = LoggingLevel.Trace
            };

            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);
        }
Exemple #2
0
        private void LogResponseLoggingInfo(HttpResponseMessage response)
        {
            var info = new ApiLoggingInfo
            {
                MessageType           = HttpMessageType.Response,
                HttpMethod            = response.RequestMessage.Method.ToString(),
                ResponseStatusCode    = response.StatusCode,
                ResponseStatusMessage = response.ReasonPhrase,
                UriAccessed           = response.RequestMessage.RequestUri.AbsoluteUri,
                IpAddress             = response.RequestMessage.GetClientIpAddress(),
                LoggingLevel          = LoggingLevel.Trace
            };

            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);
        }
        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(t =>
                {
                    var responseMsg  = System.Text.UTF8Encoding.UTF8.GetString(t.Result);
                    info.BodyContent = responseMsg;
                    _repository.Log(info);
                });

                return;
            }

            _repository.Log(info);
        }
        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 = System.Text.UTF8Encoding.UTF8.GetString(task.Result);
                    _repository.Log(info);
                });

                return;
            }

            _repository.Log(info);
        }
        public void Display(ApiLoggingInfo loggingInfo)
        {
            var message = new StringBuilder();

            message.AppendFormat(
                $"StartTime:{loggingInfo.StartTime},Method:{loggingInfo.HttpMethod},Url:{loggingInfo.UriAccessed},ResponseStatus:{loggingInfo.ResponseStatusCode},TotalTime:{loggingInfo.TotalTime}");
            message.AppendLine();
            message.AppendFormat($"Headers:{string.Join(",", loggingInfo.Headers)},Body:{loggingInfo.BodyContent}");
            connectionContext.Connection.Broadcast(message.ToString());
        }
Exemple #6
0
        public void Display(ApiLoggingInfo loggingInfo)
        {
            var message = new StringBuilder();

            //message.AppendFormat("StartTime:{0},Method:{1},Url:{2},ReponseStatus:{3},TotalTime:{4}"
            //   , loggingInfo.StartTime, loggingInfo.HttpMethod, loggingInfo.UriAccessed, loggingInfo.ResponseStatusCode, loggingInfo.TotalTime);
            //message.AppendLine();
            //message.AppendFormat("Headers:{0},Body:{1}", string.Join(",", loggingInfo.Headers), loggingInfo.BodyContent);
            message.AppendFormat("{0}", loggingInfo.BodyContent);
            connectionContext.Connection.Broadcast(message.ToString());
        }
Exemple #7
0
        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));
        }
Exemple #8
0
        private ApiLoggingInfo ExtractLoggingInfoFromRequest(HttpRequestMessage request, HttpContextWrapper context)
        {
            var info = new ApiLoggingInfo
            {
                HttpMethod  = request.Method.Method,
                UriAccessed = request.RequestUri.AbsoluteUri,
                IpAddress   = HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "0.0.0.0",
                ContentType = context.Request.ContentType
            };

            Task <string> getContentTask = request.Content.ReadAsStringAsync();

            info.BodyContent = getContentTask.Result;
            info.Headers     = request.Headers.ToString();


            return(info);
        }
Exemple #9
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()));
            });
        }
Exemple #10
0
        /// <summary>
        /// 记录请求日志信息
        /// </summary>
        /// <param name="request">http请求</param>
        private void LogRequestLoggingInfo(HttpRequestMessage request)
        {
            var info = new ApiLoggingInfo()
            {
                HttpMethod   = request.Method.Method,
                UriAccessed  = request.RequestUri.AbsoluteUri,
                IpAddress    = request.GetClientIpAddress(),
                LoggingLevel = LoggingLevel.Trace,
                StartTime    = DateTime.Now
            };

            ExtractMessageHeadersIntoLoggingInfo(info, request.Headers.ToList());

            if (request.Content != null)
            {
                request.Content.ReadAsByteArrayAsync().ContinueWith(task =>
                {
                    info.BodyContent = Encoding.UTF8.GetString(task.Result);
                });
                request.Properties.Add(_loggingInfoKey, info);
                return;
            }
            request.Properties.Add(_loggingInfoKey, info);
        }
Exemple #11
0
 /// <summary>
 /// 写日志
 /// </summary>
 /// <param name="loggingInfo">api日志信息</param>
 public void Log(ApiLoggingInfo loggingInfo)
 {
     _connectionContext.Connection.Broadcast(loggingInfo.ToJson());
 }