예제 #1
0
        public static void LogRequest(HttpActionContext actionContext)
        {
            RESTExposeContext restExposeContext = GetLoggingContext();

            if (restExposeContext != null && (restExposeContext.TraceAll || restExposeContext.TraceErrors))
            {
                HttpContext   context      = HttpContext.Current;
                StringBuilder requestTrace = new StringBuilder();
                requestTrace.AppendLine(context.Request.HttpMethod + " " + context.Request.Url.ToString() + " " + context.Request.ServerVariables["SERVER_PROTOCOL"]);
                WriteRequestHeaders(requestTrace, context);

                if (actionContext.Request.Content != null)
                {
                    byte[] content = actionContext.Request.Content.ReadAsByteArrayAsync().Result;
                    if (!restExposeContext.IsRequestBinary)
                    {
                        try {
                            string   charset         = RestServiceHttpUtils.TryGetRequestEncoding(actionContext.Request, DefaultEncoding);
                            Encoding requestEncoding = Encoding.GetEncoding(charset);
                            requestTrace.AppendLine(requestEncoding.GetString(content));
                        } catch {
                            WriteBinaryData(requestTrace);
                        }
                    }
                    else
                    {
                        WriteBinaryData(requestTrace);
                    }
                }

                restExposeContext.RequestTrace = requestTrace.ToString();
            }
        }
예제 #2
0
        public static void LogRequest(HttpActionContext actionContext)
        {
            RESTExposeContext restExposeContext = GetLoggingContext();

            if (restExposeContext != null && (restExposeContext.TraceAll || restExposeContext.TraceErrors))
            {
                HttpContext   context      = HttpContext.Current;
                StringBuilder requestTrace = new StringBuilder();
                requestTrace.AppendLine(context.Request.HttpMethod + " " + context.Request.Url.ToString() + " " + context.Request.ServerVariables["SERVER_PROTOCOL"]);
                foreach (string headerName in context.Request.Headers.Keys)
                {
                    foreach (string headerValue in context.Request.Headers.GetValues(headerName))
                    {
                        requestTrace.AppendLine(headerName + ": " + headerValue);
                    }
                }

                if (actionContext.Request.Content != null)
                {
                    byte[] content = actionContext.Request.Content.ReadAsByteArrayAsync().Result;
                    if (!restExposeContext.IsRequestBinary)
                    {
                        try {
                            string   charset         = RestServiceHttpUtils.TryGetRequestEncoding(actionContext.Request, "utf-8");
                            Encoding requestEncoding = Encoding.GetEncoding(charset);
                            requestTrace.AppendLine(requestEncoding.GetString(content));
                        } catch {
                            requestTrace.AppendLine("<BINARY DATA>");
                        }
                    }
                    else
                    {
                        requestTrace.AppendLine("<BINARY DATA>");
                    }
                }

                restExposeContext.RequestTrace = requestTrace.ToString();
            }
        }