C
C#•9mo ago
Kuno

Logging in ASP.NET

Hello, ASP.NET automatically creates ILogger<T> through the constructor, but how to create a logger for internal class, like here ?
No description
15 Replies
Kuno
KunoOP•9mo ago
for FlightRepository in this case
Saber
Saber•9mo ago
stop creating instances of your classes and let the DI system handle that for you.
Angius
Angius•9mo ago
+1 to injecting your contexts and repositories
phaseshift
phaseshift•9mo ago
But you could use a ilogger factory
Joschi
Joschi•9mo ago
This service would be registered in the DI container, that means it injects dependencies for you, as long as they are also registered. You wouldn't create your own DB Context in each class, you would constructor inject it as needed just like the ILogger. The same is true for your flight repository. Don't create it yourself. Inject it in the constructor and the DI container will take care of the dependencies
Unknown User
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Kuno
KunoOP•9mo ago
I don't use EFCore, my db_context is a regular connection to the postgre db and it's used for each repository
c#
public class DbContext
{
public NpgsqlConnection Connection { get; set; }
public DbContext(string _connectionString)
{
try
{
Connection = new NpgsqlConnection(_connectionString);
Connection.Open();
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}
}
c#
public class DbContext
{
public NpgsqlConnection Connection { get; set; }
public DbContext(string _connectionString)
{
try
{
Connection = new NpgsqlConnection(_connectionString);
Connection.Open();
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}
}
I injected IConfiguration inside my service controller to pull connection string out from there
c#
public class RefresherService : BackgroundService, IRefresherService
{
private readonly ILogger<RefresherService> _logger;
private readonly DbContext _dbContext;
private readonly Aerodrome _aerodrome;
private readonly FlightRepository _flightRepository;
private readonly string _connectionString;
private readonly IConfiguration _configuration;

public RefresherService(ILogger<RefresherService> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;

_connectionString = _configuration.GetConnectionString("PostgreSQL");
_dbContext = new(_connectionString);
_aerodrome = new Aerodrome();
_flightRepository = new(_dbContext.Connection, _aerodrome);
}
c#
public class RefresherService : BackgroundService, IRefresherService
{
private readonly ILogger<RefresherService> _logger;
private readonly DbContext _dbContext;
private readonly Aerodrome _aerodrome;
private readonly FlightRepository _flightRepository;
private readonly string _connectionString;
private readonly IConfiguration _configuration;

public RefresherService(ILogger<RefresherService> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;

_connectionString = _configuration.GetConnectionString("PostgreSQL");
_dbContext = new(_connectionString);
_aerodrome = new Aerodrome();
_flightRepository = new(_dbContext.Connection, _aerodrome);
}
I guess it's better to add IConfiguration in DbContext and let DI system to handle that
Unknown User
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Kuno
KunoOP•9mo ago
did you mean like:
c#
public class PostgreDB
{
public NpgsqlConnection Connection { get; set; }
private readonly IConfiguration _configuration;
private readonly ILogger<PostgreDB> _logger;
public PostgreDB(IConfiguration configuration, ILogger<PostgreDB> logger)
{
try
{
Connection = new NpgsqlConnection(_configuration.GetConnectionString("PostgreSQL"));
_logger = logger;
Connection.Open();
_logger.LogInformation($"[{DateTime.Now.TimeOfDay}] Connected to database");
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}
}
}

public class RefresherService : BackgroundService, IRefresherService
{
private readonly ILogger<RefresherService> _logger;
private readonly PostgreDB _dbContext;
private readonly Aerodrome _aerodrome;
private readonly FlightRepository _flightRepository;
private readonly string _connectionString;

public RefresherService(ILogger<RefresherService> logger, PostgreDB dbContext)
{
_logger = logger;
_dbContext = dbContext;
_aerodrome = new Aerodrome();
_flightRepository = new(_dbContext.Connection, _aerodrome);
}
}
c#
public class PostgreDB
{
public NpgsqlConnection Connection { get; set; }
private readonly IConfiguration _configuration;
private readonly ILogger<PostgreDB> _logger;
public PostgreDB(IConfiguration configuration, ILogger<PostgreDB> logger)
{
try
{
Connection = new NpgsqlConnection(_configuration.GetConnectionString("PostgreSQL"));
_logger = logger;
Connection.Open();
_logger.LogInformation($"[{DateTime.Now.TimeOfDay}] Connected to database");
}
catch (SqlException e)
{
Console.WriteLine(e.ToString());
}
}
}

public class RefresherService : BackgroundService, IRefresherService
{
private readonly ILogger<RefresherService> _logger;
private readonly PostgreDB _dbContext;
private readonly Aerodrome _aerodrome;
private readonly FlightRepository _flightRepository;
private readonly string _connectionString;

public RefresherService(ILogger<RefresherService> logger, PostgreDB dbContext)
{
_logger = logger;
_dbContext = dbContext;
_aerodrome = new Aerodrome();
_flightRepository = new(_dbContext.Connection, _aerodrome);
}
}
Unknown User
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Kuno
KunoOP•9mo ago
I got it
Unknown User
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Kuno
KunoOP•9mo ago
tbh, I don't use this class at all 🙂 i should get rid of it Where it has to be then ?
Unknown User
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Kuno
KunoOP•9mo ago
Ok, thanks
Want results from more Discord servers?
Add your server