Original: http://blog.bossma.cn/dotnet/asp-net-web-api-log-request-response/
ASP. NET Web API records a method of requesting response data to a log
Rest-style service architectures have become more and more of a choice, before I used WCF to implement rest services, WCF is a very powerful framework to do this little thing a bit too heavy feeling. Later, Microsoft launched an ASP. NET Web API, which is designed to handle some basic HTTP services, which are flexible and simple, and recommended for everyone to look at.
Today's article is a small experience in the process of using the ASP. NET Web API to share with you.
Background services usually need to record some running records, such as input and output, request information, processing, response information, etc., in the previous record request data and response data, I often in the specific interface method, such as:
/// Account Verification /// </summary> /// <param name= "instance" ></param> /// <returns></returns> Public loginresponse Usercheck (UserInfo instance) {loghelper.logrequest< userinfo> (instance); var response = Innerapi.userlogin (instance); Loghelper.logresponse< loginresponse> (response); return response;}
Loghelper.logrequest is used to record the requested data, and Loghelper.logresponse is used to record the returned data.
In this case, each method should be repeated so record, not concise, inconvenient, is still in the phase of copying code. Always want to change, to suffer from no train of thought.
One day in the blog park see the "ASP. NET Web API processing architecture," unsanitary environment. I started looking for a point in the Web API's life cycle that could intercept this data, and eventually found an article on the official website, HTTP Message handlers, which I re-structured to record input and output, referring to the instructions:
///<summary>///Message Handlers///</summary> Public classcustommessagehandler:delegatinghandler{///<summary> ///overriding methods for sending HTTP requests to internal handlers///</summary> ///<param name= "Request" > ask for information </param> ///<param name= "CancellationToken" > unmark the action </param> ///<returns></returns> protected Overridetask< httpresponsemessage> SendAsync (httprequestmessage request, CancellationToken CancellationToken) {//Log Request Content if(Request. Content! =NULL) {Logwriter.toinfo (string. Format ("Request Content:{0}", request. Content.readasstringasync (). Result)); }//sends an HTTP request to an internal handler, recording the response after the asynchronous processing completes return Base. SendAsync (Request, CancellationToken). continuewith< httpresponsemessage> ( (Task)=> {//Record response ContentLogwriter.toinfo (string. Format ("Response content:{0}", Task. Result.Content.ReadAsStringAsync (). Result)); returntask. Result; } ); }}
In order for message processing to enter our custom message handlers, you need to add Custommessagehandler to Messagehandlers in the Application_Start method of the Global.asax file:
protected Static void Configure (httpconfiguration config) {config. Messagehandlers.add (newprotectedvoid Application_Start (object sender, EventArgs e) {Configure (globalconfiguration.configuration);}
Delegatinghandler uses the delegate mode, so that when added, the program executes the overridden method in Custommessagehandler.
This is also an AOP a bit.
For logging, it is recommended to use log4net or nlog, mature and stable.
Please try your favorite friends quickly.
ASP. NET Web API records a method of requesting response data to a log