/* LOG */
        protected LogEntity CreateInternalLog(string message, UserEntity actor = null)
        {
            if (HttpContext?.Request == null)
            {
                return(null);
            }

            var controller = ControllerContext.RouteData.Values["controller"].ToString().FirstLetterToUpperCase();
            var action     = ControllerContext.RouteData.Values["action"].ToString();

            var actionDescriptor = MvcHelper.GetActionDescriptor(HttpContext, controller, action);
            var actionAttr       = actionDescriptor.GetCustomAttributes(typeof(CedActionAttribute), false).Cast <CedActionAttribute>().SingleOrDefault();

            EntityType?entityType = null;

            var actionTypeAttr = actionAttr?.ActionType.GetAttribute <ActionTypeAttribute>();

            if (actionTypeAttr != null)
            {
                entityType = actionTypeAttr.EntityType;
            }

            var log = new LogEntity
            {
                Url            = HttpContext.Request.RawUrl,
                ActorUserId    = actor?.UserId,
                ActorUserEmail = actor?.Email,
                Controller     = controller,
                Action         = action,
                MethodType     = ControllerContext.RequestContext.HttpContext.Request.HttpMethod,
                Ip             = HttpContext.Request.UserHostAddress,
                ActionType     = actionAttr?.ActionType,
                EntityType     = entityType,
                AdditionalInfo = message,
                CreatedOn      = DateTime.Now
            };

            log.LogId = LogServices.CreateLog(log);
            return(log);
        }
Ejemplo n.º 2
0
        public void OnException(ExceptionContext filterContext)
        {
            string message;

            if (WebConfigHelper.IsLocal || WebConfigHelper.IsTest)
            {
                message = filterContext.Exception.GetFullMessage();
            }
            else
            {
                message = "An error occured!";
            }

            if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
            {
                filterContext.ExceptionHandled = true;
                filterContext.Result           = new JsonResult
                {
                    Data = new { success = false, message = message },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }

            // DETAILED LOG
            if (!(filterContext.Controller is GlobalController))
            {
                return;
            }

            var controller = filterContext.RouteData.Values["controller"].ToString().FirstLetterToUpperCase();
            var action     = filterContext.RouteData.Values["action"].ToString();

            var actionDescriptor = MvcHelper.GetActionDescriptor(filterContext.HttpContext, controller, action);
            var actionAttr       = actionDescriptor.GetCustomAttributes(typeof(CedActionAttribute), false).Cast <CedActionAttribute>().SingleOrDefault();

            if (actionAttr == null)
            {
                return;
            }

            if (!actionAttr.Loggable)
            {
                return;
            }

            var        actionTypeAttr = actionAttr.ActionType.GetAttribute <ActionTypeAttribute>();
            EntityType?entityType     = null;

            if (actionTypeAttr != null)
            {
                entityType = actionTypeAttr.EntityType;
            }

            int?entityId = null;
            int?eventId  = null;

            if (filterContext.RouteData.Values["id"] != null)
            {
                entityId = Convert.ToInt32(filterContext.RouteData.Values["id"]);
            }

            if (entityId == null)
            {
                if (filterContext.RequestContext.HttpContext.Request.Params["EditionId"] != null)
                {
                    entityId = Convert.ToInt32(filterContext.RequestContext.HttpContext.Request.Params["EditionId"]);
                }

                if (filterContext.RequestContext.HttpContext.Request.Params["EventId"] != null)
                {
                    eventId = Convert.ToInt32(filterContext.RequestContext.HttpContext.Request.Params["EventId"]);
                }
            }

            var logMessage = "Unhandled error! " + filterContext.Exception.GetFullMessage();

            logMessage += "\r\nController: " + controller;
            logMessage += "\r\nAction: " + action;
            logMessage += "\r\nEntityId : " + entityId;
            logMessage += "\r\nEventId: " + eventId;
            logMessage += "\r\nEntityType: " + entityType;

            BusinessServices.Helpers.ExternalLogHelper.Log(logMessage, LoggingEventType.Error);
        }