7

I can inject the ILogger into a controller but I'm having an issue trying to inject it into a repository class. Here is my Startup method:

 public Startup(IHostingEnvironment env)
    {
        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables();
        Configuration = builder.Build();

        Log.Logger = new LoggerConfiguration()
     .MinimumLevel.Error()
     .WriteTo.RollingFile(Path.Combine(
         env.WebRootPath, "log-{Date}.txt"))
     .CreateLogger();}

This is in my Configure method:

   loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        loggerFactory.AddSerilog();

My Controller:

 private readonly ISkillsRepo _repo;
    private readonly ILogger _logger;

    public HistoryAndReportsController(Context db, ILogger<SkillsRepo> logger)
    {
        _repo = new SkillsRepo(db, logger);
        _logger = logger;
    }

The repo:

  private readonly Context _db;
    private readonly ILogger _logger;
    public SkillsRepo(Context db, ILogger<SkillsRepo> logger)
    {
        _db = db;
        _logger = logger;

    }

I can inject the logger into the controller and then pass it to the repo but there must be a way to inject the logger into the repo directly but I can't find any examples. I can create another logger instance in the repo but that defeats the purpose of DI. I have the same issue understanding how to do the same with the DBContext.

3
  • 1
    You can inject SkillsRepo into your controller's constructor. DI chain will automatically inject DBContext and Logger into the SkillsRepo. In this case, your SkillsRepo need to be register in Startup.cs
    – Igor
    Feb 17, 2016 at 9:50
  • 1
    Thanks Igor. I was able to get your suggestion to work after I added the repo to the ConfigureServices in the Statup class. services.AddScoped<ISkillsRepo, SkillsRepo>();
    – forwheeler
    Feb 20, 2016 at 1:13
  • Igor what if you want to pass to the constructor some string value which comes from appsettings.json, how the registration would look like in the Startup class?
    – GoldenAge
    Dec 1, 2018 at 20:02

2 Answers 2

0

As it has already been mentioned you need to register your repository in the dependency injection (DI) container first.

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddScoped<IUserRepository, AwesomeUserRepository>();
        ...
    }

ILoggerFactory (and therefore ILogger<T>) are framework provided injections and don't need to be added manually.

-1

Igor's answer was the solution with the addition of adding your repo to the services otherwise you get an error.

"Unable to resolve service for type "

1
  • Please provide a complete full solution in your answer. Feb 16, 2017 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.