Inevitably, if you’re building an API, you’re going to want to monitor requests made to that API. A big step in doing that is logging each API request so that you can capture and determine important information about your endpoints, such as data being sent in, data being returned, how many times endpoints were invoked, and how long it took endpoints to serve their requests.
ASP.NET Web API makes logging API requests straightforward with a DelegatingHandler, which is part of the message handlers responsible for processing various parts of the Web API request pipeline. Implementing your own DelegatingHandler allows you to intercept API calls so that you can take additional action, such as validating API keys, performing authentication, or in this case, logging API requests.
For this demonstration, we’re going to log the data that came in as part of the request and the data being returned as part of the response. To capture this set of data we have a class named ApiLogEntry, and this is what we’ll persist to our database.
With that in place we can take a look at the ApiLogHandler class, our custom DelegatingHandler used to intercept all API calls:
Some things to note about this class:
- It inherits from DelegatingHandler; this is important.
- It uses ContinueWith statements to help avoid potential deadlock issues.
- It uses the Newtonsoft.Json library to perform serialization.
- You have to get the calling app from somewhere, such as the Authorization header, query parameter, etc.
- You have to write the code to save the API log entry to your database.
Now that we have our custom DelegatingHandler, we need to let the Web API infrastructure know it exists, which we do by registering it in the Application_Start method of global.asax:
Done and Done
And that’s it. This pattern can be used as-is or tweaked to satisfy other needs, but as you can see, being able to add your own custom message handler (in the form of as DelegatingHandler) takes minimal effort for something that could have a big benefit to your application.