Need to register asp.net webapi 2 request and response body in database

I am using Microsoft Asp.net WebApi2 hosted in IIS. I would just like to register the request body (xml or json) and the response body for each message.

There is nothing special about this project or controller handling the message. I am not interested in using registration frameworks such as nLog, elmah, log4net or the built-in webapi trace functions if this is not necessary.

I just want to know where to put the registration code and how to get the actual json or xml from the incoming and outgoing request and response.

My mail sending method:

public HttpResponseMessage Post([FromBody])Employee employee) { if (ModelState.IsValid) { // insert employee into to database } } 
+96
c # asp.net-web-api
May 14 '14 at 16:33
source share
4 answers

I would recommend using DelegatingHandler . Then you do not need to worry about any logging protocol in your controllers.

 public class LogRequestAndResponseHandler : DelegatingHandler { protected override async Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { // log request body string requestBody = await request.Content.ReadAsStringAsync(); Trace.WriteLine(requestBody); // let other handlers process the request var result = await base.SendAsync(request, cancellationToken); if (result.Content != null) { // once response body is ready, log it var responseBody = await result.Content.ReadAsStringAsync(); Trace.WriteLine(responseBody); } return result; } } 

Just replace Trace.WriteLine with the registration code and register the handler in WebApiConfig as follows:

 config.MessageHandlers.Add(new LogRequestAndResponseHandler()); 
+178
May 14 '14 at 17:01
source share

There are several approaches to general request / response log processing for each call to the WebAPI method:

  • ActionFilterAttribute : You can write custom ActionFilterAttribute and decorate controller / action methods to enable logging.

    Con: you need to decorate each controller / methods (nevertheless, you can do this on the base controller, but still it does not address the problem of cross-references.

  • Override BaseController and register there.

    Con: We expect / force controllers to inherit from the user base controller.

  • Using DelegatingHandler .

    Advantage: We do not touch the controller / method here with this approach. The delegate handler sits in isolation and gracefully processes the request / response log.

For a more detailed article, see http://weblogs.asp.net/fredriknormen/log-message-request-and-response-in-asp-net-webapi .

+14
Jul 04 '16 at 22:28
source share

One of the options that you have is to create an action filter and design it with your WebApiController / ApiMethod.

Filter attribute

 public class MyFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext.Request.Method == HttpMethod.Post) { var postData = actionContext.ActionArguments; //do logging here } } } 

WebApi Controller

 [MyFilterAttribute] public class ValuesController : ApiController{..} 

or

 [MyFilterAttribute] public void Post([FromBody]string value){..} 

Hope this helps.

+11
May 14 '14 at 18:28
source share

Accessing the request is easy. Your base ApiController class contains a .Request property , which, as the name implies, contains a parsed request. You simply check it for everything you are looking for, to register it and transfer it to your registration object, whatever it is. This code can be entered at the beginning of the action if you need to do this only for one or more.

If you need to do this on all actions (all values ​​are larger than the managed part), then what you can do is override the .ExecuteAsync method to capture every action call for your controller.

 public override Task<HttpResponseMessage> ExecuteAsync( HttpControllerContext controllerContext, CancellationToken cancellationToken ) { // Do logging here using controllerContext.Request return base.ExecuteAsync(controllerContext, cancellationToken); } 
+3
May 14 '14 at 17:01
source share



All Articles