Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am trying to log requests in aspnet core through middleware. Basically I want to know how many one or another endpoint is being used.
For example I have 2 controller and each controller has 2 endpoints:
HTML
GET /customers/{id}
GET /customers/hard-working-customers

GET /profiles/{id}
GET /profiles/outdated-profiles

So the final result I want to count the calls. For example one endpoint was used X amount of times and another used Y amount of times.
Though it looks like there is no easy way to get controller/action from HttpContext in middleware...
Any suggestion how to achieve this in a clean manner?

Thanks!

What I have tried:

googled for possible solution without much of a luck.
Posted
Updated 16-Jan-19 22:07pm

1 solution

I got this solved by applying custom ActionFilter attribute to controllers where I want the logging to happen.
This allowed me to get the Controller and Action using this simple syntax:
{context.ActionDescriptor.RouteValues["controller"]}
{context.ActionDescriptor.RouteValues["action"]}


Resource:
Filters in ASP.NET Core | Microsoft Docs[^]
 
Share this answer
 
Comments
Maciej Los 18-Jan-19 7:05am    
+5 for your effort and sharing your solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900