public object InsertToErrorLog(Exception exception, string functionName, string userInfo)
 {
     try
     {
         if (userInfo != null)
         {
             string[] userInfos = userInfo.Split(',');
             Errorlog errorLog  = new Errorlog
             {
                 ErrorCode    = "100",
                 Message      = exception.Message.ToString(),
                 ErrorDate    = DateTime.Now,
                 FunctionName = functionName,
                 UserId       = userInfos[0],
                 RoleId       = userInfos[1]
             };
             errorLogRepository.Add(errorLog);
             return(null);
         }
         else
         {
             Errorlog errorLog = new Errorlog
             {
                 ErrorCode    = "100",
                 Message      = exception.Message.ToString(),
                 ErrorDate    = DateTime.Now,
                 FunctionName = functionName
             };
             errorLogRepository.Add(errorLog);
             return(null);
         }
     }
     catch (Exception ex)
     {
         throw;
     }
 }
        public void OnException(ExceptionContext filterContext)
        {
            //Send ajax response
            if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
            {
                filterContext.Result = new JsonResult
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = new
                    {
                        Message = "An error has occured. Please try again later.",
                    }
                };
                filterContext.HttpContext.Response.StatusCode = 500;
                filterContext.ExceptionHandled = true;
            }


            //get Injection Repository
            IErrorLogRepository repo = DependencyResolver.Current.GetService <IErrorLogRepository>();

            //Log the error
            var error = new ErrorLog()
            {
                Message        = filterContext.Exception.Message,
                StackTrace     = filterContext.Exception.StackTrace,
                ControllerName = filterContext.Controller.GetType().Name,
                TargetedResult = filterContext.Result.GetType().Name,
                SessionId      = (string)filterContext.HttpContext.Request["LoanId"],
                UserAgent      = filterContext.HttpContext.Request.UserAgent,
                Timestamp      = DateTime.Now
            };

            repo.Add(error);

            ////Send an email notification
            //MailMessage email = new MailMessage();
            //email.From = new MailAddress("*****@*****.**");
            //email.To.Add(new MailAddress(ConfigurationManager.AppSettings["ErrorEmail"]));
            //email.Subject = "An error has occured";
            //email.Body = filterContext.Exception.Message + Environment.NewLine
            //    + filterContext.Exception.StackTrace;
            //SmtpClient client = new SmtpClient("localhost");
            //client.Send(email);
        }
示例#3
0
        public void OnException(ExceptionContext context)
        {
            var currentController = string.Empty;
            var currentAction     = string.Empty;

            if (context.RouteData != null)
            {
                if (context.RouteData.Values["controller"] != null && !String.IsNullOrEmpty(context.RouteData.Values["controller"].ToString()))
                {
                    currentController = context.RouteData.Values["controller"].ToString();
                }

                if (context.RouteData.Values["action"] != null && !String.IsNullOrEmpty(context.RouteData.Values["action"].ToString()))
                {
                    currentAction = context.RouteData.Values["action"].ToString();
                }
            }

            UserAgent userAgent = null;

            if (context.HttpContext.Request.Headers != null && !string.IsNullOrEmpty(context.HttpContext.Request.Headers["User-Agent"]))
            {
                userAgent = new UserAgent(context.HttpContext.Request.Headers["User-Agent"]);
            }

            var ehErrorViewModel = new ErrorLogModel
            {
                Thread      = string.Empty,
                Level       = "Error",
                Logger      = string.Empty,
                Message     = context.Exception.Message,
                Exception   = context.Exception.StackTrace,
                UserName    = context.HttpContext.User.Identity.Name,
                Controller  = currentController,
                Action      = currentAction,
                Application = "EH.Assessment",
                EmailSent   = false,
            };

            if (userAgent != null)
            {
                ehErrorViewModel.BrowserType           = userAgent.Browser.Name;
                ehErrorViewModel.BrowserVersion        = userAgent.Browser.Version;
                ehErrorViewModel.ClientOperatingSystem = userAgent.OS.Name + "_" + userAgent.OS.Version;
            }

            if (context.HttpContext.Request.QueryString != null && context.HttpContext.Request.QueryString.HasValue)
            {
                ehErrorViewModel.QueryString = context.HttpContext.Request.QueryString.Value;
            }

            ehErrorViewModel.RequestMethod = context.HttpContext.Request.Method;
            ehErrorViewModel.RemoteHost    = context.HttpContext.Request.Host.Value;

            _errorLogRepository.Add(ehErrorViewModel);

            context.Result = new ViewResult()
            {
                ViewName = "Error"
            };
        }
示例#4
0
 public void Register(ErrorLog errorLog)
 {
     _errorLogRepository.Add(errorLog);
     _unitOfWork.Complete();
 }
示例#5
0
 public void Add(ErrorLog error)
 {
     errorLogRepository.Add(error);
 }
示例#6
0
 public ErrorLog Create(ErrorLog error)
 {
     return(_errorRepository.Add(error));
 }