ILogger in inherited Controller class

0

I have a web api controller class that is defined as usual, and uses the ILogger interface:

namespace API.Controllers
{
    [Route("api/identity")]
    [ApiController]
    public class IdentityController : ControllerBase
    {

        private readonly IConfiguration _config;
        private readonly ILogger _log;

        public IdentityController(IConfiguration configuration, ILogger<IdentityController> log)
        {
            _config = configuration;
            _log = log;
        }

And throughout the controller, I log information. This works.

Now I am adding logging to a second controller. But, unlike the above controller, and more like all my other controllers, I have created a base controller class that all my other controllers derive from. So my base controller:

namespace API.Controllers
{
    public class BaseController : ControllerBase
    {
        internal JWT ReadJWT()
        {
            ... a method common to all controllers
        }

    }
}

And then my controllers are defined:

namespace API.Controllers
{
    [ApiController]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public class AccountsController : BaseController
    {
        private readonly IAccountsService _accountService;
        private readonly ILogger _log;

        public AccountsController(ILogger log)
        {
            _log = log;
            _accountService = new AccountsService(ReadJWT());
        }

Note, that this controller, derives from BaseController, which in turn, derives from the standard ControllerBase.

This has been working fine - until I added the ILogger interface. Since adding that interface to my constructor, I get an error on application startup:

System.InvalidOperationException HResult=0x80131509 Message=Action 'API.Controllers.AccountsController.Accounts (API)' has more than one parameter that was specified or inferred as bound from request body. Only one parameter per action may be bound from body. Inspect the following parameters, and use 'FromQueryAttribute' to specify bound from query, 'FromRouteAttribute' to specify bound from route, and 'FromBodyAttribute' for parameters to be bound from body: AccountListFilterRequest request ILogger logger
Source=Microsoft.AspNetCore.Mvc.Core StackTrace: at Microsoft.AspNetCore.Mvc.ApplicationModels.InferParameterBindingInfoConvention.InferParameterBindingSources(ActionModel action) ...

Could someone guide me onto the right track here? For controllers deriving from the standard ControllerBase, it's fine, but when I use my base class, which uses ControllerBase, I have a failure.

Maybe the base controller in my case isnt required. My ReadJWT simply grabs data from HttpContext and gets the current logged in user based on their JWT in the header. Maybe I can make that an extension class? But that's another lesson all together. I should see how I can implement ILogger here.

c#
asp.net-web-api
asked on Stack Overflow Mar 1, 2020 by Craig

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0