February 24, 2017 by Anuraj
ASP.NET Core AOP
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 :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub