2

I've recently created a logging solution for my application whereby my web servers send all request/response data to a log over HTTP to a logging server. I've done this using delegating handlers

http://www.asp.net/web-api/overview/advanced/http-message-handlers

This is working very well, but I was curious about whether or not there is a way to do this in a manner that doesn't require me to log the message before it's sent back to the client. Logging the message isn't something that adds any value for the end user, and so I'd like them to not have to wait for the logging logic to finish, before getting back their data.

I guess what I'm looking for is some kind of hook that gets called AFTER the message has already been returned to the client, but before the thread disappears, and where I can still have access to the request and response.

Am I asking for too much?

Thanks!

Carlos Rodriguez
  • 2,190
  • 2
  • 18
  • 29

1 Answers1

1

You could create a separated thread that does the logging, and return the response to the client browser.

For example, if using Web API 2:

public IHttpActionResult Get(int id)
{
  //create response_data

  new System.Threading.Tasks.Task(() =>
  {
    //do the logging
  }).Start();

  return Content(HttpStatusCode.Accepted, response_data);
}