예제 #1
0
        private void FinalizeRequestProcessing(RavenBaseApiController controller, HttpResponseMessage response, Stopwatch sw)
        {
            LogHttpRequestStatsParams logHttpRequestStatsParam = null;

            try
            {
                StringBuilder sb = null;
                if (controller.CustomRequestTraceInfo != null && controller.CustomRequestTraceInfo.Count > 0)
                {
                    sb = new StringBuilder();
                    foreach (var action in controller.CustomRequestTraceInfo)
                    {
                        try
                        {
                            action(sb);
                        }
                        catch (Exception e)
                        {
                            Logger.WarnException("Could not gather information to log request stats custom info, so far got " + sb, e);
                        }
                        sb.AppendLine();
                    }
                    while (sb.Length > 0)
                    {
                        if (!char.IsWhiteSpace(sb[sb.Length - 1]))
                        {
                            break;
                        }
                        sb.Length--;
                    }
                }
                var innerRequest       = controller.InnerRequest;
                var httpRequestHeaders = innerRequest.Headers;
                var httpContentHeaders = innerRequest.Content == null ? null : innerRequest.Content.Headers;
                logHttpRequestStatsParam = new LogHttpRequestStatsParams(
                    sw,
                    new Lazy <HttpHeaders>(() => RavenBaseApiController.CloneRequestHttpHeaders(httpRequestHeaders, httpContentHeaders)),
                    controller.InnerRequest.Method.Method,
                    response != null ? (int)response.StatusCode : 500,
                    controller.InnerRequest.RequestUri.ToString(),
                    sb != null ? sb.ToString() : null,
                    controller.InnerRequestsCount
                    );
            }
            catch (Exception e)
            {
                Logger.WarnException("Could not gather information to log request stats", e);
            }

            if (logHttpRequestStatsParam == null || sw == null)
            {
                return;
            }

            sw.Stop();

            if (landlord.IsDatabaseLoaded(controller.TenantName ?? Constants.SystemDatabase))
            {
                controller.MarkRequestDuration(sw.ElapsedMilliseconds);
            }

            var curReq = Interlocked.Increment(ref reqNum);

            LogHttpRequestStats(controller, logHttpRequestStatsParam, controller.TenantName, curReq);

            if (controller.IsInternalRequest == false)
            {
                TraceTraffic(controller, logHttpRequestStatsParam, controller.TenantName);
            }

            RememberRecentRequests(logHttpRequestStatsParam, controller.TenantName);
        }
예제 #2
0
        private void FinalizeRequestProcessing(IResourceApiController controller, HttpResponseMessage response, Stopwatch sw)
        {
            LogHttpRequestStatsParams logHttpRequestStatsParam = null;

            try
            {
                StringBuilder sb = null;
                if (controller.CustomRequestTraceInfo != null && controller.CustomRequestTraceInfo.Count > 0)
                {
                    sb = new StringBuilder();
                    foreach (var action in controller.CustomRequestTraceInfo)
                    {
                        try
                        {
                            action(sb);
                        }
                        catch (Exception e)
                        {
                            Logger.WarnException("Could not gather information to log request stats custom info, so far got " + sb, e);
                        }
                        sb.AppendLine();
                    }
                    while (sb.Length > 0)
                    {
                        if (!char.IsWhiteSpace(sb[sb.Length - 1]))
                        {
                            break;
                        }
                        sb.Length--;
                    }
                }
                var innerRequest       = controller.InnerRequest;
                var httpRequestHeaders = innerRequest.Headers;
                var httpContentHeaders = innerRequest.Content == null ? null : innerRequest.Content.Headers;
                logHttpRequestStatsParam = new LogHttpRequestStatsParams(
                    sw,
                    new Lazy <HttpHeaders>(() => RavenBaseApiController.CloneRequestHttpHeaders(httpRequestHeaders, httpContentHeaders)),
                    controller.InnerRequest.Method.Method,
                    response != null ? (int)response.StatusCode : 500,
                    controller.InnerRequest.RequestUri.ToString(),
                    sb != null ? sb.ToString() : null,
                    controller.InnerRequestsCount
                    );
            }
            catch (Exception e)
            {
                Logger.WarnException("Could not gather information to log request stats", e);
            }

            if (logHttpRequestStatsParam == null || sw == null)
            {
                return;
            }

            sw.Stop();

            MarkRequestDuration(controller, sw.ElapsedMilliseconds);

            object requestNumber;

            if (controller.InnerRequest.Properties.TryGetValue("requestNum", out requestNumber) && requestNumber is long)
            {
                LogHttpRequestStats(controller, logHttpRequestStatsParam, controller.ResourceName, (long)requestNumber);
            }

            if (controller.IsInternalRequest == false)
            {
                TraceTraffic(controller, logHttpRequestStatsParam, controller.ResourceName, response);
            }

            RememberRecentRequests(logHttpRequestStatsParam, controller.ResourceName);
        }