写一个ActionFilter检测WebApi接口请求和响应
我们一般用日志记录每次Action的请求和响应,方便接口出错后排查,不过如果每个Action方法内都写操作日志太麻烦,而且客户端传递了错误JSON或XML,没法对应强类型参数,请求没法进入方法内,
把日志记录操作放在一个ActionFilter即可。
[AttributeUsageAttribute(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public class ApiActionAttribute : ActionFilterAttribute
{
private string _requestId;
public override void OnActionExecuting(HttpActionContext actionContext)
{
base.OnActionExecuting(actionContext);
_requestId = DateTime.Now.Ticks.ToString(); //获取请求数据
Stream stream = actionContext.Request.Content.ReadAsStreamAsync().Result;
string requestDataStr = "";
if (stream != null && stream.Length > )
{
stream.Position = ; //当你读取完之后必须把stream的读取位置设为开始
using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
{
requestDataStr = reader.ReadToEnd().ToString();
}
} //[POST_Request] {requestid} http://dev.localhost/messagr/api/message/send {data}
Logger.Instance.WriteLine("[{0}_Request] {1} {2}\r\n{3}", actionContext.Request.Method.Method, _requestId, actionContext.Request.RequestUri.AbsoluteUri, requestDataStr);
} public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
string responseDataStr = actionExecutedContext.Response.Content.ReadAsStringAsync().Result;
//[POST_Response] {requestid} {data}
Logger.Instance.WriteLine("[{0}_Response] {1}\r\n{2}", actionExecutedContext.Response.RequestMessage.Method, _requestId, responseDataStr);
}
}
_requestId 是用来标识请求的,根据它可以找到对应的Request与Response,便于排查。在Action上声明:
[Route("send_email")]
[HttpPost]
[ApiAction]
[ApiException]
public async Task<SendMessageResponseDto> Send(SendEmailMessageRequestDto dto)
{
//...
}
写一个ActionFilter检测WebApi接口请求和响应的更多相关文章
- Java基础-接口.编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,要求 方法 实现输出大写英文字母表的功能,printLowerca
#34.编写2个接口:InterfaceA和InterfaceB:在接口InterfaceA中有个方法void printCapitalLetter():在接口InterfaceB中有个方法void ...
- 发送一个简单的http get 请求并且响应
问题 如何发送一个简单的HTTP GET请求并且取回相应的HTTP响应. 设计 创建一个WebClient类的实例,然后使用它的DownloadData()方法. 方案 string uri = &q ...
- WebApi接口请求失败,找不到资源。
WebApi开发接口,实现同步数据库的数据给安卓. public class UserInfoController : ApiControllerBase { private UserBLL user ...
- 用GO写一个连接比特币JSONRPC接口的程序
比特币钱包默认是不开启JSONRPC接口的,要在比特币区块文件夹下新建bitcoin.conf这个文件,并写入以下内容 server=1 rpcuser=xxmm rpcpassword=1234 ...
- ASP.NET Core 入门(2)(WebApi接口请求日志 Request和Response)
以前 .NET Framework WebApi 记录接口访问日志,一般是通过Filter的方式进行拦截,通过重写ActionFilterAttribute的OnActionExecuting实现拦截 ...
- 34.编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,要求printCapitalLetter()方法 实现输出大写英文字母表的功能,
//接口InterfaceA package jieKou; public interface IInterfaceA { void printCapitalLetter(); } //接口Inter ...
- 编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,最后再在主类E 的main方法中创建Print的对象并赋值,运行方法
package lianxi; public interface InterfaceA { void printCapitalLetter(); } package lianxi; public in ...
- SoapUI接口测试·第一个HTTP Request接口请求和断言
一.新建SOAP项目 [File]-[New SOAP Project],在[Project Name]输入{工程名},点击[OK]. 二.新建TestSuite 选中项目,右键选择[New Te ...
- 打造一个自动检测页面是否存在XSS的小插件
前言: 还记得刚玩Web安全时,就想着要是能有一个自动挖掘XSS漏洞的软件就好了.然后我发现了Safe3.JSky.AWVS.Netsparker等等,但是误报太多,而且特别占内存.后来发现了fidd ...
随机推荐
- 硅谷新闻1--引导界面GuideActivity
1.红点切换间距 RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv_red_point.getLayoutPa ...
- mysql 5.7.15发布
2016-09-06,mysql发布了5.7更新5.7.15,修复的bug数项目之前的版本已经大大减少,说明越来越稳定了.估计再过三四个版本,就会有很多公司开始考虑生产中使用了.
- mac下用ruby安装sass && webstorm下给scss文件添加watch
1.安装rvm 先安装 [Xcode](http://developer.apple.com/xcode/) 开发工具,它将帮你安装好 Unix 环境需要的开发包 sudo curl -L https ...
- js中创建对象的几种方式
创建对象指创建一个object并给这个对象添加属性和方法,有以下几个方式: 最基本的: var Person={}; Person.name='tom'; Person.age='20'; Perso ...
- Uploading Files in SharePoint 2013 using CSOM and REST
http://www.shillier.com/archive/2013/03/26/uploading-files-in-sharepoint-2013-using-csom-and-rest.as ...
- vc6.0连接mysql数据库
一.MySQL的安装 Mysql的安装去官网下载就可以...最新的是5.7版本.. 二.VC6.0的设置 (1)打开VC6.中选0 工具栏Tools菜单下的Options选项,在Directories ...
- UITableView删除添加和移动
#import "RootTableViewController.h" @interface RootTableViewController () @property (nonat ...
- 在xib中添加手势控件后运行可能会出现的错误
如果出现错误: // -[UITapGestureRecognizer superview]: unrecognized selector sent to instance 0x8e407a0 // ...
- 阿里云ECS/Ubuntu下JDK、Tomcat、MySQL安装记录
今天六一儿童节,然后... ... ... ... 然后就是父亲节呀孩子们!!! ———————————————————————割———————————————————————— 同事需要JDK.To ...
- android 5.X之使用Palette
这几天为了学些android5.0版本sdk的新特性,折腾了好久.AndroidStudio被我反复的安装又卸载又安装,在eclipse和AndroidStudio 之间来回折腾.没想到sdk升级到5 ...