Trace Web API Execution Time Using Custom Action Filter

Introduction

In this article we will see how to trace the execution time of API methods using a Custom Action Filter.

Step 1

Open Visual Studio 2013. Go to the "File" menu then select "New" and click on "Project...".

Project
Step 2

Select "Web" from the left panel and "ASP.NET Web Application" from the center panel.

Provide an appropriate name for the application, select the location and click on the "OK" button.

select location

Step 3

Select "Empty" in the template list.

Check the "Web API" checkbox and click the "OK" button.

click on OK

Step 4

As we select the empty template, we need to add one controller. Right-click on Controllers, select "Add" and click on "Controller".

controller

Step 5

Select "Web API 2 Controller with read/write actions" and click the "Add" button.

add controller

Step 6

Name it "Home Controller".

home controller

Step 7

It will add a controller with 5 API methods.

API methods

Step 8

Now it's time to add a class to create the custom action filter. Right-click on the project, select "Add" and click "New Item".

add new item

Step 9

Select "Class" and provide the name "ExecutionTimeFilterAttribute".

class

Step 10

Now inherit the preceding class from "ActionFilterAttribute". The "System.Web.Http.Filters" namespace is required to add the ActionFilterAttribute class. It is a base class for all action-filter attributes. And action-filters are the way to add extra functionality to our Web API service. In this class we are overriding two methods, one is "OnActionExecuting" that occurs before the action method is invoked and another one is "OnActionExecuted" that occurs after the action method is invoked. Our main task is to trace the execution time and we have two methods out of which one is executed before and another is executed after the action method.

OnActionExecuted method

Step 11

So here we are using a Stopwatch to calculate the execution time and it requires the "System. Diagnostics" namespace. In the before method, OnActionExecuting, we set the action name property in HTTP request by initializing the stopwatch instance that sets the elapsed time property to zero and starts measuring the elapsed time. And in the after method, OnActionExecuted, we get the stopwatch instance from the same property and get the total elapsed time measured by the instance.

OnActionExecuted

Step 12

Now we need to add this (ExecutionTimeFilterAttribute) attribute to an API method, for which we want to measure the execution time. So let's add this attribute to the "Get" method.

measure execution time

Step 13

Run the application and call the "Get" method. Then check the output in the output window.

Output window

Here we can see the execution time for the "Get" method.

execution time

Step 14

Now whatever we did, that is for one method. But in a practical scenario there can be many methods in one controller and many controllers in one application, we should not add this attribute for all API methods in order to calculate the execution time. So in this case we can configure it in Global.asax.cs. We can add this custom attribute in GlobalConfiguration inside the Application_Start event.

GlobalConfiguration

Step 15

Remove the ExecutionTimeFilter attribute from the Get method as we have configured globally.

ExecutionTimeFilter

Step 16

This attribute works for all API methods now. Let's check by calling two different API methods.

attribute

You can check in the output window, the elapsed time for both of the methods.

Output


Similar Articles