feat(core): add optional SQL query logging
This commit is contained in:
parent
b95fb76cd4
commit
6aed05af06
8 changed files with 56 additions and 31 deletions
|
@ -1,5 +1,6 @@
|
|||
using Foxchat.Core;
|
||||
using Foxchat.Core.Database;
|
||||
using Foxchat.Core.Extensions;
|
||||
using Foxchat.Identity.Database.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
|
@ -10,6 +11,7 @@ namespace Foxchat.Identity.Database;
|
|||
public class IdentityContext : IDatabaseContext
|
||||
{
|
||||
private readonly NpgsqlDataSource _dataSource;
|
||||
private readonly ILoggerFactory? _loggerFactory;
|
||||
|
||||
public override DbSet<Instance> Instance { get; set; }
|
||||
public DbSet<Account> Accounts { get; set; }
|
||||
|
@ -18,7 +20,7 @@ public class IdentityContext : IDatabaseContext
|
|||
public DbSet<Token> Tokens { get; set; }
|
||||
public DbSet<GuildAccount> GuildAccounts { get; set; }
|
||||
|
||||
public IdentityContext(InstanceConfig config)
|
||||
public IdentityContext(InstanceConfig config, ILoggerFactory? loggerFactory)
|
||||
{
|
||||
var connString = new NpgsqlConnectionStringBuilder(config.Database.Url)
|
||||
{
|
||||
|
@ -29,12 +31,14 @@ public class IdentityContext : IDatabaseContext
|
|||
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
|
||||
dataSourceBuilder.UseNodaTime();
|
||||
_dataSource = dataSourceBuilder.Build();
|
||||
_loggerFactory = loggerFactory;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> optionsBuilder
|
||||
.UseNpgsql(_dataSource, o => o.UseNodaTime())
|
||||
.UseSnakeCaseNamingConvention();
|
||||
=> optionsBuilder
|
||||
.UseNpgsql(_dataSource, o => o.UseNodaTime())
|
||||
.UseSnakeCaseNamingConvention()
|
||||
.UseLoggerFactory(_loggerFactory);
|
||||
|
||||
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
|
||||
{
|
||||
|
@ -67,6 +71,6 @@ public class DesignTimeIdentityContextFactory : IDesignTimeDbContextFactory<Iden
|
|||
// Get the configuration as our config class
|
||||
.Get<InstanceConfig>() ?? new();
|
||||
|
||||
return new IdentityContext(config);
|
||||
return new IdentityContext(config, null);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue