zoukankan      html  css  js  c++  java
  • webapi使用ExceptionFilterAttribute过滤器

    文章

    public class ApiExceptionFilterAttribute:ExceptionFilterAttribute
        {
            public override void OnException(HttpActionExecutedContext actionExcutedContext)
            {
                HttpRequestMessage request = actionExcutedContext.Request;
                string controllerName = actionExcutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName;
                string actionName = actionExcutedContext.ActionContext.ActionDescriptor.ActionName;
                string content = request.Content.ReadAsStringAsync().Result;
                string exceptionMessage = actionExcutedContext.Exception.Message;
                string exceptionTrace = actionExcutedContext.Exception.StackTrace;
                string arguments = JsonConvert.SerializeObject(actionExcutedContext.ActionContext.ActionArguments);
                string url = request.RequestUri.AbsoluteUri;
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("URL:{0}<br>", url);
                sb.AppendFormat("Controller:{0}<br>", controllerName);
                sb.AppendFormat("Action:{0}<br>",actionName);
                sb.AppendFormat("Arguments:{0}<br>",arguments);
                sb.AppendFormat("Content:{0}<br>",content);
                sb.AppendFormat("ExceptionMessage:{0}<br>", exceptionMessage);
                sb.AppendFormat("ExceptionTrace:{0}<br>", exceptionTrace);
                actionExcutedContext.Response = new HttpResponseMessage() { Content = new StringContent(sb.ToString()),StatusCode=System.Net.HttpStatusCode.InternalServerError };
                //记录日志
    
                sb.Clear();
                sb = null;
                base.OnException(actionExcutedContext);
            }
        }
  • 相关阅读:
    ueditor问题解决
    Odoo内部视图格式——widget
    Odoo进销存业务学习笔记
    Odoo权限设置机制
    Odoo配置文件
    Odoo——self的使用
    Odoo 启动选项
    Yiiyii2.0将高级模板的入口文件移到根目录
    Xmanager xbrower 远程linux下的应用(pycharm phpstorm)
    mysql backup
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/12075592.html
Copyright © 2011-2022 走看看