【4858.com】应用特色捕捉万分,和归并写Log

By admin in 4858.com on 2019年4月16日

声明:本代码只适用于.NET
MVC.

声明:本办法及代码只利用与.NET
Web API.

概述: 

  ASP.NET Web API
的好用利用过的都知道,未有复杂的布署文件,二个简便的ApiController加上供给的Action就能做事。可是项目,总有越发发生,本节就来谈谈API的充裕的晤面处理和写统一写log逻辑的缓解方案。

概述: 

  ASP.NET Web API
的好用利用过的都知情,未有复杂的布局文件,3个简练的ApiController加上需求的Action就能干活。然则项目,总有十一分爆发,本节就来谈谈API的百般的联结处理和写统一写log逻辑的解决方案。

先制造1个类承继ActionFilterAttribute这一个抽象类以及落到实处IExceptionFilter接口,并达成它的方法OnException。

先创设类承袭ExceptionFilterAttribute类型并复写OnException方法。

问题:

   在ASP.NET Web
API编写时,如若每个API都写十分处理逻辑,不但加大了费用工作量,且每一种开辟职员处理相当重临的数据结构也不尽同样,在那些发闹事态下,客户端处理非凡的逻辑就不再通用,也还要加大了对接接口职员的工作量,好的API错误码和错误新闻都以固定格式,并后台应该有对应的13分记录。

【4858.com】应用特色捕捉万分,和归并写Log。问题:

   在ASP.NET Web
API编写时,若是各个API都写极度处理逻辑,不但加大了开荒职业量,且每一种开荒职员处理相当重回的数据结构也不尽一样,在相当发生景况下,客户端处理相当的逻辑就不再通用,也还要加大了对接接口职员的工作量,好的API错误码和错误音信都以固定格式,并后台应该有相应的不胜记录。

代码如下:

代码如下:

可怜的联结处理的兑现:

一.
首先定义格外处理Attribute,承继System.Web.Http.Filters.ExceptionAttribute,
重写OnException, 代码如下

4858.com 14858.com 2

 1   public class ErrorHandleAttribute : System.Web.Http.Filters.ExceptionFilterAttribute
 2     {
 3         private string _msg = string.Empty;
 4 
 5         public ErrorHandleAttribute() { }
 6 
 7         public ErrorHandleAttribute(string msg)
 8         {
 9             this._msg = msg;
10         }
11         public override void OnException(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
12         {
13             base.OnException(actionExecutedContext);
14             // 取得发生异常时的错误讯息
15             //var errorMessage = actionExecutedContext.Exception.Message;
16             // 标记log
17             var logAction = actionExecutedContext.ActionContext.ActionDescriptor.GetCustomAttributes<NoErrorHandlerAttribute>();
18             if (logAction.Any())
19             {
20                 actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, new ResultData(ResultType.SystemException, actionExecutedContext.Exception.Message));
21                 return;
22             }
23 
24             var request = HttpContext.Current.Request;
25             var logDetail = new LogDetail
26             {
27                 //获取action名称
28                 ActionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName,
29                 //获取Controller 名称
30                 ControllerName = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName,
31                 Navigator = request.UserAgent,
32                 //获取访问的ip
33                 IP = request.UserHostAddress,
34                 UserHostName = request.UserHostName,
35                 UrlReferrer = request.UrlReferrer != null ? request.UrlReferrer.AbsoluteUri : "",
36                 Browser = request.Browser.Browser + " - " + request.Browser.Version + " - " + request.Browser.Type,
37                 //获取request提交的参数
38                 Paramaters = GetRequestValues(actionExecutedContext),
39                 //获取response响应的结果
40                 //ExecuteResult = GetResponseValues(actionExecutedContext),  //这句会报错,异常没有处理结果
41                 AttrTitle = this._msg,
42                 ErrorMsg = string.Format("错误信息:{0}, 异常跟踪:{1}", actionExecutedContext.Exception.Message, actionExecutedContext.Exception.StackTrace),
43                 RequestUri = request.Url.AbsoluteUri
44             };
45 
46             // 写log
47             var logRep = ContainerManager.Resolve<ISysLogRepository>();
48             var log = new Log()
49             {
50                 Action = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName + "/" + actionExecutedContext.ActionContext.ActionDescriptor.ActionName,
51                 CreateDate = DateTime.Now,
52                 CreatorLoginName = RISContext.Current.CurrentUserInfo.UserName,
53                 IpAddress = request.UserHostAddress,
54                 Detail = Utility.JsonSerialize<LogDetail>(logDetail)
55             };
56 
57             logRep.Add(log);
58             actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, new ResultData(ResultType.SystemException, actionExecutedContext.Exception.Message));
59         }
60         
61         /// <summary>
62         /// 读取request 的提交内容
63         /// </summary>
64         /// <param name="actionExecutedContext"></param>
65         /// <returns></returns>
66         public string GetRequestValues(HttpActionExecutedContext actionExecutedContext)
67         {
68 
69             Stream stream = actionExecutedContext.Request.Content.ReadAsStreamAsync().Result;
70             Encoding encoding = Encoding.UTF8;
71             /*
72                 这个StreamReader不能关闭,也不能dispose, 关了就傻逼了
73                 因为你关掉后,后面的管道  或拦截器就没办法读取了
74             */
75             var reader = new StreamReader(stream, encoding);
76             string result = reader.ReadToEnd();
77             /*
78             这里也要注意:   stream.Position = 0;
79             当你读取完之后必须把stream的位置设为开始
80             因为request和response读取完以后Position到最后一个位置,交给下一个方法处理的时候就会读不到内容了。
81             */
82             stream.Position = 0;
83             return result;
84         }
85     }

View Code

  1. 接下去定义不须要格外处理的Attribute,代码如下:

    1 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true)]
    2 public class NoErrorHandlerAttribute : Attribute
    3 {
    4 }

  2. 在HttpConfiguration中注册使用 ErrorHandleAttribute, 注册代码如下:

  config.Filters.Add(new ErrorHandleAttribute(“错误处理”));

  1般在类型的WebApiConfig.cs中登记此属性:

 1  /// <summary>
 2     /// WebApiConfig
 3     /// </summary>
 4     public static class WebApiConfig
 5     {
 6         /// <summary>
 7         /// WebApiConfig Register
 8         /// </summary>
 9         /// <param name="config"></param>
10         public static void Register(HttpConfiguration config)
11         {
12             //config.Filters.Add(new TokenAuthorizeAttribute());
13             config.MessageHandlers.Add(new CrosHandler());
14             config.Filters.Add(new ApiAuthorizeAttribute());
15             config.Filters.Add(new ErrorHandleAttribute("错误处理"));
16             // Web API 路由
17             config.Routes.MapHttpRoute(
18                 name: "DefaultApi",
19                 routeTemplate: "mobileapi/{controller}/{action}/{id}",
20                 defaults: new { controller = "Test", action = "GetTestValue", id = RouteParameter.Optional }
21             );
22         }
23     }

如此那般就足以了,在各类Action中就毫无写try
catch了,不然不施行ErrorHandle中尤其处理逻辑
4.
万111分规的Controller可能Action不要求纪录和拍卖相当,能够在Controller大概Action上加多[NoErrorHandler],那样就不会推行ErrorHandle中十二分处理逻辑

上述部分是老大的合并处理逻辑, 接下来贯彻统一写Log的 Attribute功效

非凡的联合处理的落实:

一.
率先定义极度处理Attribute,承袭System.Web.Http.Filters.ExceptionAttribute,
重写OnException, 代码如下

4858.com 34858.com 4

 1   public class ErrorHandleAttribute : System.Web.Http.Filters.ExceptionFilterAttribute
 2     {
 3         private string _msg = string.Empty;
 4 
 5         public ErrorHandleAttribute() { }
 6 
 7         public ErrorHandleAttribute(string msg)
 8         {
 9             this._msg = msg;
10         }
11         public override void OnException(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
12         {
13             base.OnException(actionExecutedContext);
14             // 取得发生异常时的错误讯息
15             //var errorMessage = actionExecutedContext.Exception.Message;
16             // 标记log
17             var logAction = actionExecutedContext.ActionContext.ActionDescriptor.GetCustomAttributes<NoErrorHandlerAttribute>();
18             if (logAction.Any())
19             {
20                 actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, new ResultData(ResultType.SystemException, actionExecutedContext.Exception.Message));
21                 return;
22             }
23 
24             var request = HttpContext.Current.Request;
25             var logDetail = new LogDetail
26             {
27                 //获取action名称
28                 ActionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName,
29                 //获取Controller 名称
30                 ControllerName = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName,
31                 Navigator = request.UserAgent,
32                 //获取访问的ip
33                 IP = request.UserHostAddress,
34                 UserHostName = request.UserHostName,
35                 UrlReferrer = request.UrlReferrer != null ? request.UrlReferrer.AbsoluteUri : "",
36                 Browser = request.Browser.Browser + " - " + request.Browser.Version + " - " + request.Browser.Type,
37                 //获取request提交的参数
38                 Paramaters = GetRequestValues(actionExecutedContext),
39                 //获取response响应的结果
40                 //ExecuteResult = GetResponseValues(actionExecutedContext),  //这句会报错,异常没有处理结果
41                 AttrTitle = this._msg,
42                 ErrorMsg = string.Format("错误信息:{0}, 异常跟踪:{1}", actionExecutedContext.Exception.Message, actionExecutedContext.Exception.StackTrace),
43                 RequestUri = request.Url.AbsoluteUri
44             };
45 
46             // 写log
47             var logRep = ContainerManager.Resolve<ISysLogRepository>();
48             var log = new Log()
49             {
50                 Action = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName + "/" + actionExecutedContext.ActionContext.ActionDescriptor.ActionName,
51                 CreateDate = DateTime.Now,
52                 CreatorLoginName = RISContext.Current.CurrentUserInfo.UserName,
53                 IpAddress = request.UserHostAddress,
54                 Detail = Utility.JsonSerialize<LogDetail>(logDetail)
55             };
56 
57             logRep.Add(log);
58             actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, new ResultData(ResultType.SystemException, actionExecutedContext.Exception.Message));
59         }
60         
61         /// <summary>
62         /// 读取request 的提交内容
63         /// </summary>
64         /// <param name="actionExecutedContext"></param>
65         /// <returns></returns>
66         public string GetRequestValues(HttpActionExecutedContext actionExecutedContext)
67         {
68 
69             Stream stream = actionExecutedContext.Request.Content.ReadAsStreamAsync().Result;
70             Encoding encoding = Encoding.UTF8;
71             /*
72                 这个StreamReader不能关闭,也不能dispose, 关了就傻逼了
73                 因为你关掉后,后面的管道  或拦截器就没办法读取了
74             */
75             var reader = new StreamReader(stream, encoding);
76             string result = reader.ReadToEnd();
77             /*
78             这里也要注意:   stream.Position = 0;
79             当你读取完之后必须把stream的位置设为开始
80             因为request和response读取完以后Position到最后一个位置,交给下一个方法处理的时候就会读不到内容了。
81             */
82             stream.Position = 0;
83             return result;
84         }
85     }

View Code

  1. 4858.com ,接下去定义不供给极度处理的Attribute,代码如下:

    1 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true)]
    2 public class NoErrorHandlerAttribute : Attribute
    3 {
    4 }

  2. 在HttpConfiguration中注册使用 ErrorHandleAttribute, 注册代码如下:

  config.Filters.Add(new ErrorHandleAttribute(“错误处理”));

  一般在档次的WebApiConfig.cs中登记此属性:

 1  /// <summary>
 2     /// WebApiConfig
 3     /// </summary>
 4     public static class WebApiConfig
 5     {
 6         /// <summary>
 7         /// WebApiConfig Register
 8         /// </summary>
 9         /// <param name="config"></param>
10         public static void Register(HttpConfiguration config)
11         {
12             //config.Filters.Add(new TokenAuthorizeAttribute());
13             config.MessageHandlers.Add(new CrosHandler());
14             config.Filters.Add(new ApiAuthorizeAttribute());
15             config.Filters.Add(new ErrorHandleAttribute("错误处理"));
16             // Web API 路由
17             config.Routes.MapHttpRoute(
18                 name: "DefaultApi",
19                 routeTemplate: "mobileapi/{controller}/{action}/{id}",
20                 defaults: new { controller = "Test", action = "GetTestValue", id = RouteParameter.Optional }
21             );
22         }
23     }

这么就足以了,在每一个Action中就毫无写try
catch了,不然不试行ErrorHandle中国和澳洲常处理逻辑
4.
比方非凡规的Controller或然Action不供给纪录和拍卖至极,能够在Controller可能Action上增加[NoErrorHandler],那样就不会试行ErrorHandle中尤其处理逻辑

上述部分是充裕的会面处理逻辑, 接下来贯彻合并写Log的 Attribute成效

using FrameWork.Common;
using System.Web;
using System.Web.Mvc;

namespace FrameWork.Web
{
    public class ErrorHandleAttribute : ActionFilterAttribute, IExceptionFilter
    {
        /// <summary>
        /// 异常
        /// </summary>
        public void OnException(ExceptionContext filterContext)
        {
            //获取异常信息,入库保存
            var error = filterContext.Exception;
            var message = error.Message;//错误信息
            var url = HttpContext.Current.Request.RawUrl;//错误发生地址
            var controllerName = filterContext.RouteData.Values["controller"].ToString();
            var actionName = filterContext.RouteData.Values["action"].ToString();
            var exception = filterContext.Exception.ToString();

            Log4NetHelp.Error("出错位置:" + url, filterContext.Exception);
            filterContext.HttpContext.Response.Redirect("/Account/Login");//否则跳转至登陆页
        }
    }
}
using System;using System.IO;using System.Net.Http;using System.Text;using System.Web.Http.Filters;using FrameWork.Common;using FrameWork.Common.Const;namespace FrameWork.Web.Handle{    /// <summary>    /// 处理错误信息    /// </summary>    public class ErrorHandleAttribute : ExceptionFilterAttribute    {        public override void OnException(HttpActionExecutedContext filterContext)        {            // 错误处理            base.OnException(filterContext);            var sb = new StringBuilder();            sb.AppendLine("参数:"+ GetExceptionMessage(filterContext));            sb.AppendLine("异常内容:"+ filterContext.Exception.ToJson;            Log4NetHelp.Error(sb.ToString;            filterContext.Response = GetResponse(sb.ToString;        }        /// <summary>        /// 处理错误信息为方便开发人员阅读的格式,包括接口地址以及参数        /// </summary>        /// <param name="actionExecutedContext">异常内容</param>        private string GetExceptionMessage(HttpActionExecutedContext actionExecutedContext)        {            var session = System.Web.HttpContext.Current.Session;            var request = System.Web.HttpContext.Current.Request;            var guid = Guid.NewGuid().ToString();            var task = actionExecutedContext.ActionContext.Request.Content.ReadAsStreamAsync();            var content = string.Empty;            try            {                var sm = task.Result;                if (sm != null)                {                    sm.Seek(0, SeekOrigin.Begin);                    var len = (int)sm.Length;                    var inputByts = new byte[len];                    sm.Read(inputByts, 0, len);                    sm.Close();                    content = Encoding.UTF8.GetString(inputByts);                    sm.Close();                }            }            catch (Exception e)            {                return e.Message;            }            var sessionId = session == null ? "" : session.SessionID;            var pars = string.Format("error:\r\n id = {3};\r\n sessionId = {0};\r\n url = {1};\r\n contentType = {4};\r\n content = {2};"                , sessionId                , request.RawUrl                , content                , guid                , request.ContentType);            return pars;        }        /// <summary>        /// 处理错误信息后执行的方法        /// </summary>        /// <param name="mes">错误信息</param>        private HttpResponseMessage GetResponse(string mes)        {            return JsonHelper.ToJson(new            {                Info = CommonConst.FailStr,                Message = mes,                Msg = false,                ResultCode = CommonConst.FailCode            });        }    }}

统壹写Log的 Attribute功效达成:

 一.
率先定义写Log的Attribute,承袭System.Web.Http.Filters.ActionFilterAttribute,重写OnActionExecuting和OnActionExecuted,代码如下:

4858.com 54858.com 6

  1  public class LogAttribute : ActionFilterAttribute
  2     {
  3         private string _msg = string.Empty;
  4         private string _token = string.Empty;
  5         private string _remark = string.Empty;
  6         public LogAttribute() { }
  7 
  8         public LogAttribute(string msg)
  9         {
 10             this._msg = msg;
 11         }
 12 
 13         //http://www.cnblogs.com/shan333chao/p/5002054.html
 14         private static readonly string key = "enterTime";
 15         private const string UserToken = "token";
 16         public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
 17         {
 18             if (actionContext.Request.Method != HttpMethod.Options)
 19             {
 20                 // 标记log
 21                 var logAction = actionContext.ActionDescriptor.GetCustomAttributes<NoLogAttribute>();
 22                 if (!logAction.Any())
 23                 {
 24                     actionContext.Request.Properties[key] = DateTime.Now.ToBinary();
 25                     this._token = GetToken(actionContext, out this._remark);
 26                 }
 27             }
 28             base.OnActionExecuting(actionContext);
 29         }
 30 
 31         public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
 32         {
 33             if (actionExecutedContext.Request.Method != HttpMethod.Options)
 34             {
 35                 object beginTime = null;
 36                 if (actionExecutedContext.Request.Properties.TryGetValue(key, out beginTime))
 37                 {
 38                     DateTime time = DateTime.FromBinary(Convert.ToInt64(beginTime));
 39                     var request = HttpContext.Current.Request;
 40                     var logDetail = new LogDetail
 41                     {
 42                         //获取action名称
 43                         ActionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName,
 44                         //获取Controller 名称
 45                         ControllerName = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName,
 46                         //获取action开始执行的时间
 47                         EnterTime = time,
 48                         //获取执行action的耗时
 49                         CostTime = (DateTime.Now - time).TotalMilliseconds,
 50                         Navigator = request.UserAgent,
 51                         Token = this._token,
 52                         //获取用户ID
 53                         UId = UserTokenManager.GetUId(this._token),
 54                         //获取访问的ip
 55                         IP = request.UserHostAddress,
 56                         UserHostName = request.UserHostName,
 57                         UrlReferrer = request.UrlReferrer != null ? request.UrlReferrer.AbsoluteUri : "",
 58                         Browser = request.Browser.Browser + " - " + request.Browser.Version + " - " + request.Browser.Type,
 59                         //获取request提交的参数
 60                         Paramaters = GetRequestValues(actionExecutedContext),
 61                         //获取response响应的结果
 62                         ExecuteResult = GetResponseValues(actionExecutedContext),
 63                         AttrTitle = this._msg,
 64                         Remark = this._remark,
 65                         RequestUri = request.Url.AbsoluteUri
 66                     };
 67 
 68                     // 登录log
 69                     var logRep = ContainerManager.Resolve<ISysLogRepository>();
 70                     var log = new Log()
 71                     {
 72                         Action = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName + "/" + actionExecutedContext.ActionContext.ActionDescriptor.ActionName,
 73                         CreateDate = DateTime.Now,
 74                         CreatorLoginName = RISContext.Current.CurrentUserInfo.UserName,
 75                         IpAddress = request.UserHostAddress,
 76                         Detail = Utility.JsonSerialize<LogDetail>(logDetail)
 77                     };
 78 
 79                     logRep.Add(log);
 80                 }
 81             }
 82 
 83             base.OnActionExecuted(actionExecutedContext);
 84         }
 85 
 86         private string GetToken(System.Web.Http.Controllers.HttpActionContext actionContext, out string msg)
 87         {
 88             Dictionary<string, object> actionArguments = actionContext.ActionArguments;
 89             HttpMethod type = actionContext.Request.Method;
 90             msg = "";
 91             var token = "";
 92             if (type == HttpMethod.Post)
 93             {
 94                 if (actionArguments.ContainsKey(UserToken))
 95                 {
 96                     if (actionArguments[UserToken] != null)
 97                         token = actionArguments[UserToken].ToString();
 98                 }
 99                 else
100                 {
101                     foreach (var value in actionArguments.Values)
102                     {
103                         if (value != null && value.GetType().GetProperty(UserToken) != null)
104                             token = value.GetType().GetProperty(UserToken).GetValue(value, null).ToString();
105                     }
106                 }
107 
108                 if (string.IsNullOrEmpty(token))
109                     msg = "匿名用户";
110             }
111             else if (type == HttpMethod.Get)
112             {
113                 if (!actionArguments.ContainsKey(UserToken))
114                     msg = "匿名用户";
115                 // throw new HttpException(401, "还未登录");
116 
117                 if (actionArguments[UserToken] != null)
118                     token = actionArguments[UserToken].ToString();
119                 else
120                     msg = "匿名用户";
121             }
122             else if (type == HttpMethod.Options)
123             {
124 
125             }
126             else
127             {
128                 throw new HttpException(404, "暂未开放除POST,GET之外的访问方式!");
129             }
130             return token;
131         }
132         /// <summary>
133         /// 读取request 的提交内容
134         /// </summary>
135         /// <param name="actionExecutedContext"></param>
136         /// <returns></returns>
137         public string GetRequestValues(HttpActionExecutedContext actionExecutedContext)
138         {
139 
140             Stream stream = actionExecutedContext.Request.Content.ReadAsStreamAsync().Result;
141             Encoding encoding = Encoding.UTF8;
142             /*
143                 这个StreamReader不能关闭,也不能dispose, 关了就傻逼了
144                 因为你关掉后,后面的管道  或拦截器就没办法读取了
145             */
146             var reader = new StreamReader(stream, encoding);
147             string result = reader.ReadToEnd();
148             /*
149             这里也要注意:   stream.Position = 0;
150             当你读取完之后必须把stream的位置设为开始
151             因为request和response读取完以后Position到最后一个位置,交给下一个方法处理的时候就会读不到内容了。
152             */
153             stream.Position = 0;
154             return result;
155         }
156 
157         /// <summary>
158         /// 读取action返回的result
159         /// </summary>
160         /// <param name="actionExecutedContext"></param>
161         /// <returns></returns>
162         public string GetResponseValues(HttpActionExecutedContext actionExecutedContext)
163         {
164             Stream stream = actionExecutedContext.Response.Content.ReadAsStreamAsync().Result;
165             Encoding encoding = Encoding.UTF8;
166             /*
167             这个StreamReader不能关闭,也不能dispose, 关了就傻逼了
168             因为你关掉后,后面的管道  或拦截器就没办法读取了
169             */
170             var reader = new StreamReader(stream, encoding);
171             string result = reader.ReadToEnd();
172             /*
173             这里也要注意:   stream.Position = 0; 
174             当你读取完之后必须把stream的位置设为开始
175             因为request和response读取完以后Position到最后一个位置,交给下一个方法处理的时候就会读不到内容了。
176             */
177             stream.Position = 0;
178             return result;
179         }
180     }

View Code

  1. 接下去定义不要求记录log的Attribute,代码如下:

    1 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true)]
    2 public class NoErrorHandlerAttribute : Attribute
    3 {
    4 }

  2. 专注不要在HttpConfiguration中注册使用
    LogAttribute,除非您想具备的伸手都写log,在不要求写log的Action上增加[NoLog],不然只要求在急需记录log的Action增多[Log]就能够形成写log的功效。

此篇到此甘休,相对相比较轻便,欢迎大家座谈!

 

统1写Log的 Attribute功能达成:

 1.
第①定义写Log的Attribute,承继System.Web.Http.Filters.ActionFilterAttribute,重写OnActionExecuting和OnActionExecuted,代码如下:

4858.com 74858.com 8

  1  public class LogAttribute : ActionFilterAttribute
  2     {
  3         private string _msg = string.Empty;
  4         private string _token = string.Empty;
  5         private string _remark = string.Empty;
  6         public LogAttribute() { }
  7 
  8         public LogAttribute(string msg)
  9         {
 10             this._msg = msg;
 11         }
 12 
 13         //http://www.cnblogs.com/shan333chao/p/5002054.html
 14         private static readonly string key = "enterTime";
 15         private const string UserToken = "token";
 16         public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
 17         {
 18             if (actionContext.Request.Method != HttpMethod.Options)
 19             {
 20                 // 标记log
 21                 var logAction = actionContext.ActionDescriptor.GetCustomAttributes<NoLogAttribute>();
 22                 if (!logAction.Any())
 23                 {
 24                     actionContext.Request.Properties[key] = DateTime.Now.ToBinary();
 25                     this._token = GetToken(actionContext, out this._remark);
 26                 }
 27             }
 28             base.OnActionExecuting(actionContext);
 29         }
 30 
 31         public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
 32         {
 33             if (actionExecutedContext.Request.Method != HttpMethod.Options)
 34             {
 35                 object beginTime = null;
 36                 if (actionExecutedContext.Request.Properties.TryGetValue(key, out beginTime))
 37                 {
 38                     DateTime time = DateTime.FromBinary(Convert.ToInt64(beginTime));
 39                     var request = HttpContext.Current.Request;
 40                     var logDetail = new LogDetail
 41                     {
 42                         //获取action名称
 43                         ActionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName,
 44                         //获取Controller 名称
 45                         ControllerName = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName,
 46                         //获取action开始执行的时间
 47                         EnterTime = time,
 48                         //获取执行action的耗时
 49                         CostTime = (DateTime.Now - time).TotalMilliseconds,
 50                         Navigator = request.UserAgent,
 51                         Token = this._token,
 52                         //获取用户ID
 53                         UId = UserTokenManager.GetUId(this._token),
 54                         //获取访问的ip
 55                         IP = request.UserHostAddress,
 56                         UserHostName = request.UserHostName,
 57                         UrlReferrer = request.UrlReferrer != null ? request.UrlReferrer.AbsoluteUri : "",
 58                         Browser = request.Browser.Browser + " - " + request.Browser.Version + " - " + request.Browser.Type,
 59                         //获取request提交的参数
 60                         Paramaters = GetRequestValues(actionExecutedContext),
 61                         //获取response响应的结果
 62                         ExecuteResult = GetResponseValues(actionExecutedContext),
 63                         AttrTitle = this._msg,
 64                         Remark = this._remark,
 65                         RequestUri = request.Url.AbsoluteUri
 66                     };
 67 
 68                     // 登录log
 69                     var logRep = ContainerManager.Resolve<ISysLogRepository>();
 70                     var log = new Log()
 71                     {
 72                         Action = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName + "/" + actionExecutedContext.ActionContext.ActionDescriptor.ActionName,
 73                         CreateDate = DateTime.Now,
 74                         CreatorLoginName = RISContext.Current.CurrentUserInfo.UserName,
 75                         IpAddress = request.UserHostAddress,
 76                         Detail = Utility.JsonSerialize<LogDetail>(logDetail)
 77                     };
 78 
 79                     logRep.Add(log);
 80                 }
 81             }
 82 
 83             base.OnActionExecuted(actionExecutedContext);
 84         }
 85 
 86         private string GetToken(System.Web.Http.Controllers.HttpActionContext actionContext, out string msg)
 87         {
 88             Dictionary<string, object> actionArguments = actionContext.ActionArguments;
 89             HttpMethod type = actionContext.Request.Method;
 90             msg = "";
 91             var token = "";
 92             if (type == HttpMethod.Post)
 93             {
 94                 if (actionArguments.ContainsKey(UserToken))
 95                 {
 96                     if (actionArguments[UserToken] != null)
 97                         token = actionArguments[UserToken].ToString();
 98                 }
 99                 else
100                 {
101                     foreach (var value in actionArguments.Values)
102                     {
103                         if (value != null && value.GetType().GetProperty(UserToken) != null)
104                             token = value.GetType().GetProperty(UserToken).GetValue(value, null).ToString();
105                     }
106                 }
107 
108                 if (string.IsNullOrEmpty(token))
109                     msg = "匿名用户";
110             }
111             else if (type == HttpMethod.Get)
112             {
113                 if (!actionArguments.ContainsKey(UserToken))
114                     msg = "匿名用户";
115                 // throw new HttpException(401, "还未登录");
116 
117                 if (actionArguments[UserToken] != null)
118                     token = actionArguments[UserToken].ToString();
119                 else
120                     msg = "匿名用户";
121             }
122             else if (type == HttpMethod.Options)
123             {
124 
125             }
126             else
127             {
128                 throw new HttpException(404, "暂未开放除POST,GET之外的访问方式!");
129             }
130             return token;
131         }
132         /// <summary>
133         /// 读取request 的提交内容
134         /// </summary>
135         /// <param name="actionExecutedContext"></param>
136         /// <returns></returns>
137         public string GetRequestValues(HttpActionExecutedContext actionExecutedContext)
138         {
139 
140             Stream stream = actionExecutedContext.Request.Content.ReadAsStreamAsync().Result;
141             Encoding encoding = Encoding.UTF8;
142             /*
143                 这个StreamReader不能关闭,也不能dispose, 关了就傻逼了
144                 因为你关掉后,后面的管道  或拦截器就没办法读取了
145             */
146             var reader = new StreamReader(stream, encoding);
147             string result = reader.ReadToEnd();
148             /*
149             这里也要注意:   stream.Position = 0;
150             当你读取完之后必须把stream的位置设为开始
151             因为request和response读取完以后Position到最后一个位置,交给下一个方法处理的时候就会读不到内容了。
152             */
153             stream.Position = 0;
154             return result;
155         }
156 
157         /// <summary>
158         /// 读取action返回的result
159         /// </summary>
160         /// <param name="actionExecutedContext"></param>
161         /// <returns></returns>
162         public string GetResponseValues(HttpActionExecutedContext actionExecutedContext)
163         {
164             Stream stream = actionExecutedContext.Response.Content.ReadAsStreamAsync().Result;
165             Encoding encoding = Encoding.UTF8;
166             /*
167             这个StreamReader不能关闭,也不能dispose, 关了就傻逼了
168             因为你关掉后,后面的管道  或拦截器就没办法读取了
169             */
170             var reader = new StreamReader(stream, encoding);
171             string result = reader.ReadToEnd();
172             /*
173             这里也要注意:   stream.Position = 0; 
174             当你读取完之后必须把stream的位置设为开始
175             因为request和response读取完以后Position到最后一个位置,交给下一个方法处理的时候就会读不到内容了。
176             */
177             stream.Position = 0;
178             return result;
179         }
180     }

View Code

  1. 接下去定义不须求记录log的Attribute,代码如下:

    1 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true)]
    2 public class NoErrorHandlerAttribute : Attribute
    3 {
    4 }

  2. 注意不要在HttpConfiguration中登记使用
    LogAttribute,除非您想有所的央求都写log,在不须要写log的Action上增添[NoLog],不然只必要在须要记录log的Action增多[Log]就可以成功写log的效果。

此篇到此结束,绝相比较轻易,欢迎我们座谈!

 

哪些使用啊?

怎么着运用啊?

只必要在急需捕捉相当的调控器加上[ErrorHandle]即可。

只必要在须要捕捉至极的调整器加上[ErrorHandle]即可。

例如:

例如:

    [ErrorHandle]
    public abstract class AdminControllerBase : Controller
    {
          //代码逻辑
    }
    [ErrorHandle]    public abstract class AdminControllerBase : Controller    {          //代码逻辑    }

1旦一而再AdminControllerBase大概隐含[ErrorHandle]特色的类和措施出现了错误,都会实行OnException方法,

假设后续AdminControllerBase可能隐含[ErrorHandle]性格的类和艺术出现了不当,都会施行OnException方法。

然后写上自身的不得了处理逻辑就可以。

能够在拍卖12分的格局里面增加自身想要的逻辑处理。

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有