Log all request of Web API 2 (DelegatingHandler vs ActionFilterAttribute)
-
Searching on the web I found differents solutions to log all requests received by a IIS server. - Solution 1: [DelegatingHandler][1] - Solution 2: [ActionFilterAttribute][2] - Solution 3: [The IIS log][3] I read all articles about these differents solutions and I already implemented and tested them. I don't know wich one to choose between solution 1 and solution 2. Is there an history or performance or architecture maintenance reason that explain a solution is better than another. Are DelegatingHandler and/or ActionFilterAttribute a old feature? [1]: http://stackoverflow.com/a/23660832/196526 [2]: http://www.codeproject.com/Articles/1028416/RESTful-Day-sharp-Request-logging-and-Exception-ha [3]: https://msdn.microsoft.com/en-us/library/ms525410(v=vs.90).aspx
B413
-
Searching on the web I found differents solutions to log all requests received by a IIS server. - Solution 1: [DelegatingHandler][1] - Solution 2: [ActionFilterAttribute][2] - Solution 3: [The IIS log][3] I read all articles about these differents solutions and I already implemented and tested them. I don't know wich one to choose between solution 1 and solution 2. Is there an history or performance or architecture maintenance reason that explain a solution is better than another. Are DelegatingHandler and/or ActionFilterAttribute a old feature? [1]: http://stackoverflow.com/a/23660832/196526 [2]: http://www.codeproject.com/Articles/1028416/RESTful-Day-sharp-Request-logging-and-Exception-ha [3]: https://msdn.microsoft.com/en-us/library/ms525410(v=vs.90).aspx
B413
If you want to log every request, then use the IIS log. That will include requests which might not be mapped to the Web API pipeline, and therefore wouldn't be picked up by a
DelegatingHandler
or action filter. If you just want to log the Web API requests, then either aDelegatingHandler
or an action filter will work. Neither is an "old" feature. However, if you want to log the request and response bodies as a single entry, theDelegatingHandler
would be the better choice.protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
await LogAsync(request, response);
return response;
}If you just want trace logs for your code, have a look at Tracing in ASP.NET Web API 2[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer