Logging controller and action method names
We can often make debugging a large API easier by logging the exact name of the action method called and what controller class the endpoint is a part of. Often, the action method name and Controller
class should be obvious from the request path, but in a large API, especially one that has drifted from disciplined naming conventions, including the method and class name of the endpoint can make our life significantly easier. Luckily, ASP.NET Core has a built-in ControllerActionDescriptor
class in the Microsoft.AspNetCore.Mvc.Controllers
namespace that we can use with the GetMetadata<T>()
method to easily display this information.
Getting ready
The starter project can be found here: https://github.com/PacktPublishing/ASP.NET-9-Web-API-Cookbook/tree/main/start/chapter05/LogControllerAndAction.
Make sure you have Seq and Docker set up, as described in the Technical requirements section at the beginning of this chapter.