Hi @Amir Saleem ,
If you want to log the infomation before action is called,you could custom ActionFilter to meet your requirement.
Here is a working demo:
1.Custom ActionFilter:
public class CustomFilter: IActionFilter
{
private readonly ILogger<CustomFilter> _logger;
public CustomFilter(ILogger<CustomFilter> logger)
{
_logger = logger;
}
public void OnActionExecuting(ActionExecutingContext context)
{
var controllerName = ((Microsoft.AspNetCore.Mvc.ControllerBase)context.Controller)
.ControllerContext
.ActionDescriptor
.ControllerName;
var actionName = ((Microsoft.AspNetCore.Mvc.ControllerBase)context.Controller)
.ControllerContext
.ActionDescriptor
.ActionName;
_logger.LogInformation("Get into "+actionName+" action in "+ controllerName + " controller");
}
public void OnActionExecuted(ActionExecutedContext context)
{
}
}
2.Register the filter:
services.AddControllers(
config =>
{
config.Filters.Add<CustomFilter>();
});
3.Result:
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
Rena