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.