Aspect-oriented programming in .NET Core
Aspect-oriented programming(AOP) is based on Object-oriented programming(OOP) concepts to extend concepts. It separates the main code to record the log messages, fix error messages or other messages recorded in files or other server space, and let the source code coupling level reduce in the project. These log messages can include performance information, event logs, transformation messages, etc. This best example is the filter of the ASP.NET MVC.
It has more third party tools can provide to do, such as PostSharp and Unity. Now, I use Microsoft.Extensions.Logging package to implement AOP in ASP.NET Core project is based on the Onion architecture(Building MVC pattern project by Visual Studio 2017 ( ASP.Net Core Part )).
In the AOP of the web application project, I will focus on the action filter of the ASP.NET Core and the action filter of the ASP.NET MVC. The action filter default order of execution is from sequence 1 to sequence 6.
| Sequence | Filter scope | Filter method |
|---|---|---|
| 1 | Global | OnActionExecuting |
| 2 | Controller or Razor page | OnActionExecuting |
| 3 | Method | OnActionExecuting |
| 4 | Method | OnActionExecuted |
| 5 | Controller or Razor page | OnActionExecuted |
| 6 | Global | OnActionExecuted |
Microsoft.Extensions.Logging tutorial
Step 1. Create the Logging folder under the web application project. Right-click on the web project -> Add -> New folder.

Step 2. Create the two folders under the Logging folder. One folder name is “Interface.” Another folder name is “Repository.”

2 Comments