0

I want to log the response content of requests to my service, and I'm wondering what is the best way to go about it. Here are my specific requirements:

  1. Response content should be serialized ONCE in the entire pipeline.
  2. Response content only, no headers, etc.
  3. Logging is applied to subset of all APIs.

In short, I want to add logging for a subset of all APIs while adding the least possible amount of overhead. The reason I don't want to add logging for all APIs is because we have a load balancer hitting one of the APIs repeatedly, and it is not useful for us to log those requests.

The only examples I have seen that meet the first requirement use IHttpModule and replace the filter (e.g. Logging raw HTTP request/response in ASP.NET MVC & IIS7). However using IHttpModule will log all API responses, which does not meet requirement 3. Also replacing the filter will not easily meet requirement 2 (where do the headers end and content begin?).

Due to requirements 2 and 3, I am leaning towards ActionFilterAttribute and IFilterProvider for logging (e.g. http://haacked.com/archive/2011/04/25/conditional-filters.aspx/), but I haven't seen a way to meet requirement 1 in that scenario.

A hack would be to return the HTTP content as serialized StringContent instead of ObjectContent, but this violates features in ASP.NET such as choosing your AcceptType.

Community
  • 1
  • 1
Ryan Burbidge
  • 192
  • 3
  • 13
  • What kind of "service" is this? – John Saunders Mar 31 '15 at 01:25
  • That's a very high level question. What are you looking for? It's a Web API. – Ryan Burbidge Apr 01 '15 at 17:34
  • I meant what technologies are you using? Whatever solution is offered will depend on the technologies you are using. You've given no useful information. – John Saunders Apr 01 '15 at 17:43
  • Questions saying "I want x" don't get good responses on [so]. A [so] version of this question might be, "I tried x and I got the following exception". You might possibly do better on [programmers.se], but please take the time to search there so you can see how the standards of that site differ from those of [so]. – John Saunders Apr 02 '15 at 20:44
  • http://stackoverflow.com/questions/1555969/traditional-logging-vs-aop-logging – durron597 Apr 02 '15 at 21:01
  • Thanks John. I tried to form my question in a brief manner while still fleshing out the options I have already tried, such as IHttpModule, ActionFilters, and IFilterProvider :) I specific those requirements because there are a lot of answers on the web and StackOverflow that meet one or two of those requirements, but don't do everything I'd hoped for. Sorry that my question came off as an "I want." I'll take your advice and try to form my questions in a more appropriate way in the future. – Ryan Burbidge Apr 02 '15 at 21:41

0 Answers0