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 }