I have an application and pass some dependency injection things (logger for example):
ILog _logger;
_logger = new NLogService.NLogService();
if (serviceType == typeof(OCRAPIController))
return new OCRAPIController(_logger);
Domain.ILog _logger;
public OCRAPIController(Domain.ILog logger)
{
if (logger == null)
throw new ArgumentNullException(nameof(logger));
_logger = logger;
}
and then use it:
_logger.AddLog(...);
it works and works fine. Problem is with attributes. I.e. I have the following attribute:
public class ReportAPIAccessAttribute : ActionFilterAttribute
{
public override async Task OnActionExecutingAsync(HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken)
{
and I want to log something. Of course, I can't pass ILog as parameter for constructor. I can create static method as wrapper around ILog and call it or even create NLogService.NLogService() directly in attribute. But I don't like both approaches because I want to have only one place in application, where I call NLogService directly. I can create ServiceLocator for attribute, but then other developers (or even I when I forget) can call ServiceLocator in places, where DI parameter should be called...
How to solve this problem correctly?