'how to use ILogger when using global ActionFilter

i have an action filter where i am trying to log name of action that system enters and left.

         public class LogFilter : IAsyncActionFilter
        {
            private readonly ILogger<LogFilter > _logger;
            public LogFilter(ILogger<LogFilter > logger)
            {
                _logger = logger;
            } 
            public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
            {

                _logger.LogInformation("Entering " + context.ActionDescriptor.ToString());
                // execute any code before the action executes
                var result = await next();
                // execute any code after the action executes
                _logger.LogInformation("Exiting " + context.ActionDescriptor.ToString());
            }
        }

but when i have to register this action Filter globally

 services.AddControllers(config =>
        {
            config.Filters.Add(new LogFilter (how to pass ilogger));
        });

what should i pass in the constructor ?



Solution 1:[1]

Raas, You can add this filter by type. It works for me.

services.AddControllers(config =>
        {
            config.Filters.Add(typeof(LogFilter)); // add filter By type
        });

Solution 2:[2]

There is a better way to do this:

services.AddControllers(config =>
    {
        config.Filters.Add<LogFilter>();
    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Wang
Solution 2 Gabriel Ribeiro