public void OnActionExecuted(ActionExecutedContext context)
        {
            _stopwatch.Stop();

            double elapsedMilliseconds = _stopwatch.Elapsed.TotalMilliseconds;

            _logger.Trace(context.ActionDescriptor.DisplayName, new LogItem("Event", "Controller Action Finished Executing"), new LogItem("DurationMilliseconds", elapsedMilliseconds));
            _apiMetrics.RecordHandlerDuration(elapsedMilliseconds, context.ActionDescriptor.DisplayName, (HttpStatusCode)context.HttpContext.Response.StatusCode);
        }
Example #2
0
        public IHttpResponse HandleRequest(IHttpRequest request, TokenState tokenState)
        {
            IHttpResponse response = _container.GetInstance <IHttpResponse>(true);

            if (tokenState == TokenState.Invalid || tokenState == TokenState.Expired || tokenState == TokenState.NotYetValid)
            {
                return(UnauthorisedResponse);
            }

            logger.Trace($"{nameof(RequestBroker)}.{nameof(HandleRequest)}", new LogItem("Event", "GetRequestHandler started"));
            Stopwatch timer = Stopwatch.StartNew();

            IActivityHandler handler = GetRequestHandler(request, out ActivityHandlerAttribute attribute);

            var handlerName = handler != null?handler.GetType().Name : "null";

            logger.Trace($"{nameof(RequestBroker)}.{nameof(HandleRequest)}", new LogItem("Event", "GetRequestHandler completed"), new LogItem("DurationMilliseconds", timer.Elapsed.TotalMilliseconds), new LogItem("FoundHandler", handlerName));

            if (handler == null)
            {
                switch (request.Verb)
                {
                case HttpVerb.Options:
                    handler   = _container.GetInstance <OptionsActivityHandler>();
                    attribute = new ActivityHandlerAttribute("", request.Verb, "")
                    {
                        SkipAuthorisation = true
                    };
                    break;

                default:
                    return(null);
                }
            }

            JwtSecurityToken token = request.SecurityToken;

            if (!activityAuthorisationManager.CheckAuthorisation(token, attribute))
            {
                var unauthorised = _container.GetInstance <IHttpResponse>(true);
                unauthorised.HttpStatusCode = HttpStatusCode.Unauthorized;
                return(unauthorised);
            }

            logger.Trace($"{nameof(RequestBroker)}.{nameof(HandleRequest)}", new LogItem("Event", "Handler Handle called"));
            timer.Restart();

            try
            {
                handler.Handle(request, response);
            }
            catch (Exception exception)
            {
                logger.Exception($"{nameof(RequestBroker)}.{nameof(HandleRequest)}", "Exception caught handling request", exception);

                response.SetStringContent(string.Empty);
                response.HttpStatusCode = HttpStatusCode.InternalServerError;
            }

            var elapsedMilliseconds = timer.Elapsed.TotalMilliseconds;

            logger.Trace($"{nameof(RequestBroker)}.{nameof(HandleRequest)}", new LogItem("Event", "Handler Handle completed"), new LogItem("DurationMilliseconds", elapsedMilliseconds));
            apiMetrics.RecordHandlerDuration(elapsedMilliseconds, handlerName, response.HttpStatusCode);

            return(response);
        }