Esempio n. 1
0
        //<summary>Se ejecuta cuando se  produce una excepcion, esta lee los datos de la excepcion y genera un log en la base de datos.</summary>
        /// <param name="actionExecutedContext">Contexto de ejecucion.</param>
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                IRepository <ApiLog> repository = new Repository <ApiLog>(unitOfWork);
                apiLogService = new ApiLogService(repository);

                string requestUuid = actionExecutedContext.Request.GetCorrelationId().ToString();
                //unitOfWork.BeginTransaction();
                try
                {
                    ApiLog requestLog = apiLogService.FindBy(x => x.Uuid == requestUuid).FirstOrDefault();
                    if (requestLog != null)
                    {
                        requestLog.Exception = actionExecutedContext.Exception.GetExceptionDetails();
                        apiLogService.Update(requestLog);
                    }
                    //unitOfWork.Commit();
                }
                catch (Exception e)
                {
                    //unitOfWork.Rollback();
                }
                base.OnException(actionExecutedContext);
            }
        }
Esempio n. 2
0
        //<summary>Recibe la peticion del servidor antes de que llegue al controlador para realzar el log de la peticion.</summary>
        /// <param name="cancellationToken"></param>
        /// <param name="request">Objeto que contiene informacion de las peticiones.</param>
        /// <returns>Respuesta del servidor</returns>
        protected override async System.Threading.Tasks.Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            using (IUnitOfWork unitOfWork = new UnitOfWork())
            {
                IRepository <ApiLog> repository     = new Repository <ApiLog>(unitOfWork);
                IRepository <User>   repositoryUser = new Repository <User>(unitOfWork);
                apiLogService = new ApiLogService(repository);
                userService   = new UserService(repositoryUser);

                StringBuilder                 content                = new StringBuilder();
                string                        requestUuid            = request.GetCorrelationId().ToString();
                HttpConfiguration             config                 = GlobalConfiguration.Configuration;
                IHttpRouteData                routeData              = config.Routes.GetRouteData(request);
                HttpControllerContext         controllerContext      = new HttpControllerContext(config, routeData, request);
                DefaultHttpControllerSelector controllerSelector     = new DefaultHttpControllerSelector(config);
                HttpControllerDescriptor      controllerDescriptor   = controllerSelector.SelectController(request);
                ApiControllerActionSelector   apiControllerSelection = new ApiControllerActionSelector();
                controllerContext.ControllerDescriptor = controllerDescriptor;
                HttpActionDescriptor actionDescriptor = apiControllerSelection.SelectAction(controllerContext);
                ApiLog requestLog = new ApiLog();
                requestLog.Uuid       = requestUuid;
                requestLog.Controller = controllerDescriptor.ControllerName;
                requestLog.Action     = actionDescriptor.ActionName;
                //Obtiene y decodifica el post para que se muestre la informacion de la manera adecuada
                string data = HttpUtility.UrlDecode(request.Content.ReadAsStringAsync().Result);
                requestLog.Data      = data;
                requestLog.CreatedAt = DateTime.Now;
                requestLog.Url       = request.RequestUri.AbsoluteUri;//request.RequestUri.Host + HttpUtility.UrlDecode(request.RequestUri.PathAndQuery);
                requestLog.Method    = request.Method.Method;
                requestLog.Status    = "Started";
                object headers = request.Headers.ToDictionary(x => x.Key, y => y.Value);
                requestLog.Headers = JsonConvert.SerializeObject(headers);
                //unitOfWork.BeginTransaction();
                try
                {
                    apiLogService.Create(requestLog);
                    //unitOfWork.Commit();
                }
                catch (Exception e)
                {
                    //unitOfWork.Rollback();
                }
                //Se espera la respuesta del controlador
                var response = await base.SendAsync(request, cancellationToken);

                requestUuid = request.GetCorrelationId().ToString();
                //unitOfWork.BeginTransaction();
                try
                {
                    requestLog = apiLogService.FindBy(x => x.Uuid == requestUuid).FirstOrDefault();
                    User user   = null;
                    int? userId = UserApiAuthenticated.GetUserAuthenticatedId(request.GetRequestContext());
                    if (userId.HasValue)
                    {
                        user = userService.GetById(userId.Value);
                    }
                    if (user != null)
                    {
                        requestLog.CreatedBy = user;
                    }
                    if (requestLog != null)
                    {
                        if (response.RequestMessage != null && response.Content != null)
                        {
                            var responseContent = await response.Content.ReadAsStringAsync();

                            if (requestLog.Exception != null)
                            {
                                requestLog.Status = "HasException";
                            }
                            else
                            {
                                requestLog.Status = "Completed";
                            }
                            requestLog.Result = responseContent.ToString();
                            apiLogService.Update(requestLog);
                        }
                    }
                    //unitOfWork.Commit();
                }catch (Exception e)
                {
                    //unitOfWork.Rollback();
                }
                return(response);
            }
        }