fix: better logging
- verbose logging for log channel resolving logic - don't LOG TOKENS TO THE CONSOLE OR SEQ - actually log verbose logs to seq - report open db connections to prometheus
This commit is contained in:
parent
7749c9d9e2
commit
5157105c35
7 changed files with 109 additions and 50 deletions
|
|
@ -24,18 +24,13 @@ namespace Catalogger.Backend.Database;
|
|||
|
||||
public class DatabasePool
|
||||
{
|
||||
private readonly ILogger _rootLogger;
|
||||
private readonly ILogger _logger;
|
||||
private readonly NpgsqlDataSource _dataSource;
|
||||
|
||||
private static int _openConnections;
|
||||
public static int OpenConnections => _openConnections;
|
||||
|
||||
public DatabasePool(Config config, ILogger logger, ILoggerFactory? loggerFactory)
|
||||
public DatabasePool(Config config, ILoggerFactory? loggerFactory)
|
||||
{
|
||||
_rootLogger = logger;
|
||||
_logger = logger.ForContext<DatabasePool>();
|
||||
|
||||
var connString = new NpgsqlConnectionStringBuilder(config.Database.Url)
|
||||
{
|
||||
Timeout = config.Database.Timeout ?? 5,
|
||||
|
|
@ -49,27 +44,10 @@ public class DatabasePool
|
|||
_dataSource = dataSourceBuilder.Build();
|
||||
}
|
||||
|
||||
public async Task<DatabaseConnection> AcquireAsync(CancellationToken ct = default)
|
||||
{
|
||||
return new DatabaseConnection(
|
||||
LogOpen(),
|
||||
_rootLogger,
|
||||
await _dataSource.OpenConnectionAsync(ct)
|
||||
);
|
||||
}
|
||||
public async Task<DatabaseConnection> AcquireAsync(CancellationToken ct = default) =>
|
||||
new(await _dataSource.OpenConnectionAsync(ct));
|
||||
|
||||
public DatabaseConnection Acquire()
|
||||
{
|
||||
return new DatabaseConnection(LogOpen(), _rootLogger, _dataSource.OpenConnection());
|
||||
}
|
||||
|
||||
private Guid LogOpen()
|
||||
{
|
||||
var connId = Guid.NewGuid();
|
||||
_logger.Verbose("Opening database connection {ConnId}", connId);
|
||||
IncrementConnections();
|
||||
return connId;
|
||||
}
|
||||
public DatabaseConnection Acquire() => new(_dataSource.OpenConnection());
|
||||
|
||||
public async Task ExecuteAsync(
|
||||
Func<DatabaseConnection, Task> func,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue