/// <summary>
        /// Raises the exception event.
        /// </summary>
        /// <param name="actionExecutedContext">The context for the action.</param>
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext == null || actionExecutedContext.Exception == null)
            {
                return;
            }

            MultibannerContext.SetContext(actionExecutedContext.Request);

            logger = new ApplicationInsightsLogger();
            logger.LogException(actionExecutedContext.Exception);

            var type = LookUpException(actionExecutedContext.Exception).GetType();

            Tuple <HttpStatusCode?, Func <Exception, HttpRequestMessage, HttpResponseMessage> > registration;

            if (Handlers.TryGetValue(type, out registration))
            {
                var statusCode = registration.Item1;
                var handler    = registration.Item2;

                var response = handler(
                    actionExecutedContext.Exception.GetBaseException(),
                    actionExecutedContext.Request
                    );

                // Use registered status code if available
                if (statusCode.HasValue)
                {
                    response.StatusCode = statusCode.Value;
                    //Comment this line if we want to show the error message in reason phrase also.
                    response.ReasonPhrase = statusCode.ToString().Replace(Environment.NewLine, String.Empty);
                }

                actionExecutedContext.Response = response;
            }
            else
            {
                // If no exception handler registered for the exception type, fallback to default handler
                actionExecutedContext.Response = DefaultHandler(
                    actionExecutedContext.Exception.GetBaseException(), actionExecutedContext.Request
                    );
            }

            if (actionExecutedContext.Exception != null)
            {
                CommonFunctions.LogResponse(actionExecutedContext);
            }
            base.OnException(actionExecutedContext);
        }
        public static void LogResponse(HttpActionExecutedContext actionExecutedContext)
        {
            MultibannerContext.SetContext(actionExecutedContext.Request);

            if (actionExecutedContext.Response != null)
            {
                actionExecutedContext.Response.Headers.Add("Returned-By", "Tjx.Multibanner");
                actionExecutedContext.Response.Headers.Add("Correlation-Id",
                                                           actionExecutedContext.Request.GetCorrelationId().ToString());
            }

            string requestPath =
                actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName + "_" +
                actionExecutedContext.ActionContext.ActionDescriptor.ActionName;

            Dictionary <string, string> requestHeaders = new Dictionary <string, string>();

            if (actionExecutedContext.Request.Headers.Any())
            {
                foreach (var item in actionExecutedContext.Request.Headers)
                {
                    requestHeaders.Add(item.Key, string.Join(",", item.Value));
                }
            }

            Dictionary <string, string> responseHeaders = new Dictionary <string, string>();

            if (actionExecutedContext.Response != null && actionExecutedContext.Response.Headers.Any())
            {
                foreach (var item in actionExecutedContext.Response.Headers)
                {
                    responseHeaders.Add(item.Key, string.Join(",", item.Value));
                }
            }

            Logger.LogRequest(requestPath, actionExecutedContext.Request.Method,
                              actionExecutedContext.Request.RequestUri,
                              actionExecutedContext.Response?.StatusCode.ToString() ?? "Unknown",
                              actionExecutedContext.Response?.IsSuccessStatusCode ?? false, requestHeaders, null,
                              actionExecutedContext.Request.Content.ReadAsStringAsync().Result,
                              responseHeaders,
                              (actionExecutedContext.Response != null && actionExecutedContext.Response.Content != null)
                    ? actionExecutedContext.Response.Content.ReadAsStringAsync().Result
                    : string.Empty);
        }
Esempio n. 3
0
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            _stopwatch = Stopwatch.StartNew();

            MultibannerContext.SetContext(actionContext.Request);
            var context = new MultibannerContext
            {
                CorrelationId = MultibannerContext.Current.CorrelationId,
            };
            //log request in to application insights
            //CommonFunctions.LogRequest(actionContext);
            string requestPath =
                actionContext.ControllerContext.ControllerDescriptor.ControllerName + "_" +
                actionContext.ActionDescriptor.ActionName;

            CommonFunctions.LogMetrics(requestPath + " started");
            base.OnActionExecuting(actionContext);
        }