Exemple #1
0
        public void OnException(ExceptionContext filterContext)
        {
            var logger = NLog.LogManager.GetLogger(HttpUtility.UrlDecode(filterContext.HttpContext.Request.Url.PathAndQuery, Encoding.UTF8));

            try
            {
                var errorMsg = ExceptionUtil.GetStackMessage(filterContext.Exception);
                logger.Error(errorMsg);
                var model = new ExceptionLog
                {
                    Path           = HttpUtility.UrlDecode(filterContext.HttpContext.Request.Url.AbsolutePath, Encoding.UTF8),
                    InputParameter = HttpUtil.GetInputPara(),
                    Message        = errorMsg,
                    StackTrace     = filterContext.Exception.StackTrace,
                    ClientIP       = HttpUtil.RequestHostAddress,
                    CreatedAt      = DateTime.Now
                };
                LogService.AddExceptionLog(model);
                if (filterContext.HttpContext.Request.HttpMethod == "POST")
                {
                    filterContext.ExceptionHandled = true;
                    filterContext.Result           = new NewtonsoftJsonResult()
                    {
                        Data = new BaseOutput {
                            msg = errorMsg
                        }, IncludeNotValid = true
                    };
                }
            }
            catch (Exception e)
            {
                logger.Fatal(ExceptionUtil.GetStackMessage(e));
            }
        }
        /// <summary>
        /// Hata mesajını db'ye yazdıktan sonra hata response'u oluşturur
        /// </summary>
        /// <param name="data"></param>
        /// <param name="ee"></param>
        /// <returns></returns>
        public ResponseModel SendExceptionResponse(Exception ee)
        {
            LogService logService = new LogService(this.ConnectionString);

            StringBuilder sb = new StringBuilder();

            sb.AppendLine(Request.ToString());

            sb.AppendLine("");

            sb.AppendLine("##Headers##");
            if (Request.Headers != null)
            {
                foreach (var header in Request.Headers)
                {
                    sb.AppendLine("Key:" + header.Key);
                    sb.AppendLine("Values:");
                    header.Value.ToList().ForEach(p =>
                    {
                        sb.AppendLine(p);
                        sb.AppendLine("");
                    });
                    sb.AppendLine("-----------------");
                }
            }

            sb.AppendLine("");

            sb.AppendLine("##Content Headers##");
            if (HttpContext.Request.Headers != null)
            {
                foreach (var header in HttpContext.Request.Headers)
                {
                    sb.AppendLine("Key:" + header.Key);
                    sb.AppendLine("Values:");
                    header.Value.ToList().ForEach(p =>
                    {
                        sb.AppendLine(p);
                        sb.AppendLine("");
                    });
                    sb.AppendLine("-----------------");
                }
            }

            logService.AddExceptionLog(sb.ToString(), ee.Message, CurrentUser == null ? (int?)null : CurrentUser.UserID);

            return(new ResponseModel(null, ResponseStatus.Error));
        }