1.创建一个类名字叫做CustomActionFilter.cs
////// /// public class CustomActionFilter : System.Web.Http.Filters.ActionFilterAttribute { ////// /// /// /// ///public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) { HttpRequest request = HttpContext.Current.Request; string token = request.Headers["token"]; string strUserName = null; try { strUserName = AesUtil.Decrypt(token, UserInformation.strGuid); } catch (Exception ex) { throw new Exception($"token:【{token}】无效"); } if (string.IsNullOrWhiteSpace(strUserName)) throw new Exception($"token:【{token}】无效"); var obj= DependencyInjection.Container.ICustomTableOperations.userInformation(strUserName); if (obj.IntCode == ReturnState.失败) throw new Exception(obj.strErr); // 获取request提交的参数 var Paramaters = GetRequestValues(actionExecutedContext); // 获取response响应的结果 var ExecuteResult = GetResponseValues(actionExecutedContext); dynamic data = JsonConvert.DeserializeObject(ExecuteResult); // 获取访问的ip var UserHostAddress = request.UserHostAddress; //请求地址 var path = request.AppRelativeCurrentExecutionFilePath; return base.OnActionExecutedAsync(actionExecutedContext, cancellationToken); } public string GetRequestValues(HttpActionExecutedContext actionExecutedContext) { Stream stream = actionExecutedContext.Request.Content.ReadAsStreamAsync().Result; stream.Position = 0; Encoding encoding = Encoding.UTF8; /* 这个StreamReader不能关闭,也不能dispose 因为你关掉后,后面的管道 或拦截器就没办法读取了 */ var reader = new StreamReader(stream, encoding); string result = reader.ReadToEnd(); /* 这里也要注意: stream.Position = 0; 当你读取完之后必须把stream的位置设为开始 因为request和response读取完以后Position到最后一个位置,交给下一个方法处理的时候就会读不到内容了。 */ stream.Position = 0; return result; } public string GetResponseValues(HttpActionExecutedContext actionExecutedContext) { Stream stream = actionExecutedContext.Response.Content.ReadAsStreamAsync().Result; stream.Position = 0; Encoding encoding = Encoding.UTF8; /* 这个StreamReader不能关闭,也不能dispose, 因为你关掉后,后面的管道 或拦截器就没办法读取了 */ var reader = new StreamReader(stream, encoding); string result = reader.ReadToEnd(); /* 这里也要注意: stream.Position = 0; 当你读取完之后必须把stream的位置设为开始 因为request和response读取完以后Position到最后一个位置,交给下一个方法处理的时候就会读不到内容了。 */ stream.Position = 0; return result; } }
2.在控制器上方加上
[CustomActionFilter]
这样就可以方便打印日志啦
猜你喜欢
- 4天前(大理悦云雅阁酒店电话)雅阁酒店集团|端午佳节礼遇,大理悦云雅阁度假酒店
- 4天前(哈弗h9优惠9万是真的吗)热浪来袭,哈弗H9超值补贴火热加码
- 4天前(万达酒店及度假村连续五年荣获“中国饭店集团60强”)万达酒店及度假村连续五年荣获“中国饭店集团60强”
- 4天前(七尚酒店百度百科)Lohkah七尚酒店首度开创充满新知的闽地研学旅程
- 4天前(重庆恐龙化石遗址)重庆黔江恐龙化石抢救性发掘新闻发布会举行
- 4天前(札幌小樽市)2024年暑期飞往北海道避暑吧!札幌小樽city walk路线推荐
- 4天前(曼谷丽思卡尔顿公寓价格)曼谷丽思卡尔顿酒店盛大启幕,开创泰国奢华雅致新纪元
- 4天前(岭南东方大酒店)粤西成势 | 阳江阳春长兴岭南东方酒店正式签约,粤西文旅再添明珠
- 4天前(“三天跨两城”催生租车新需求,神州租车清明跨城订单同比增长416%)“三天跨两城”催生租车新需求,神州租车清明跨城订单同比增长416%
- 4天前(芜宣机场国际航班)新华丝路:芜宣机场开通至越南首都河内的国际货运航线
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章