Posted by Anuraj on Friday, February 24, 2017 Reading time :1 minute
This post is about implementing simple AOP (Aspect Oriented Programming) with ASP.NET Core. AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code (an advice) without modifying the code itself. An example of crosscutting concerns is “logging,” which is frequently used in distributed applications to aid debugging by tracing method calls. AOP helps you to implement logging without affecting you actual code.
To add AOP to the controllers, first you need to create ActionFilter class, which should inherit from
ActionFilterAttribute class and need to override methods. Here is minimal implementation of Logging filter.
You can get various parameters and values from the
Context object, which is passed to the overriden methods. And you can use it in the Controllers as attribute like this.
Also you need to inject it using ASP.NET Core DI to use it in ServiceFilter attribute. You can do this like this.
Here is the log output from Console.
Happy Programming :)
What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.