ASP.NET 4.7.1 Hosting – Custom Filter In MVC Or Logging Using MVC Filter

I want to create a filter in MVC project which logs activity of each and every controller action. I am just injecting an object of type ILogger using which I am logging the logs in my Azure table storage.

This blog shows how to create action filter and how we can add it globally or in any controller or in any action.

Create an Action filter class:

How to make any controller to use this action filter:

Method 1

If you want the functionality to enable for all controllers by default

In starup.cs write the below line:


If you want to use it in Controller specifically:

In starup.cs write the below line:


In Action Method or controller use the below attribute: