Beispiel #1
0
        public override void OnException(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
        {
            HttpStatusCode statusCode = HttpStatusCode.BadRequest;

            if (actionExecutedContext.ActionContext.Response != null)
            {
                statusCode = actionExecutedContext.ActionContext.Response.StatusCode;
            }
            base.OnException(actionExecutedContext);
            ApiResultModel result = new ApiResultModel();

            // 取得由 API 返回的状态代码
            result.Status  = statusCode;
            result.Err     = actionExecutedContext.Exception.FullMessage();
            result.Content = new ApiResponse()
            {
                Code       = -1,
                Err        = actionExecutedContext.Exception.FullMessage(),
                StackTrace = actionExecutedContext.Exception.FullStackTrace()
            };
            MonitorLog MolLog = null;

            if (actionExecutedContext.Request.Properties.ContainsKey(Constants.Custom_LogInfoKey))
            {
                MolLog = actionExecutedContext.Request.Properties[Constants.Custom_LogInfoKey] as MonitorLog;
            }
            if (null != MolLog)
            {
                LoggerHelper.Error(MolLog.GetLoginfo());
            }
            else
            {
                LoggerHelper.Error(actionExecutedContext.Exception.FullMessage());
            }
            actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(result.Status, result);
        }
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            MonitorLog MonLog = null;

            try
            {
                object         content    = null;
                HttpStatusCode statusCode = HttpStatusCode.BadRequest;
                if (actionExecutedContext.ActionContext.Response != null)
                {
                    statusCode = actionExecutedContext.ActionContext.Response.StatusCode;
                    if (actionExecutedContext.ActionContext.Response.Content != null)
                    {
                        content = actionExecutedContext.ActionContext.Response.Content.ReadAsAsync <object>().Result;
                    }
                }
                MonLog = actionExecutedContext.Request.Properties[Constants.Custom_LogInfoKey] as MonitorLog;
                if (null != MonLog)
                {
                    MonLog.EndTime = DateTime.Now;
                    if (null != content)
                    {
                        MonLog.Response = JsonConvert.SerializeObject(content, new IsoDateTimeConverter()
                        {
                            DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
                        });
                    }
                    LoggerHelper.Info(MonLog.GetLoginfo());
                }
                // 若发生例外则不在这边处理
                if (actionExecutedContext.Exception != null)
                {
                    return;
                }
                base.OnActionExecuted(actionExecutedContext);

                ApiResultModel result = new ApiResultModel();
                // 取得由 API 返回的状态代码
                result.Status = statusCode;
                if (statusCode != HttpStatusCode.OK)
                {
                    HttpError error = content as HttpError;
                    if (error != null && error.Count > 0)
                    {
                        result.Err = error.Message;
                    }
                }
                // 取得由 API 返回的资料

                result.Content = content;
                // 重新封装回传格式
                actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(result.Status, result);
            }
            catch (Exception e)
            {
                if (null != MonLog)
                {
                    MonLog.Response = e.FullMessage();
                    actionExecutedContext.Request.Properties[Constants.Custom_LogInfoKey] = MonLog;
                }
                throw;
            }
        }