From 07880fedb5e6637ef71a44d65add71d7a63877fc Mon Sep 17 00:00:00 2001 From: glax Date: Tue, 1 Jul 2025 23:00:47 +0200 Subject: [PATCH] Create TrangaBaseContext for common OnConfiguring implementation of Contexts --- API/Schema/Contexts/LibraryContext.cs | 14 +------------- API/Schema/Contexts/NotificationsContext.cs | 13 +------------ API/Schema/Contexts/PgsqlContext.cs | 13 +------------ API/Schema/Contexts/TrangaBaseContext.cs | 19 +++++++++++++++++++ 4 files changed, 22 insertions(+), 37 deletions(-) create mode 100644 API/Schema/Contexts/TrangaBaseContext.cs diff --git a/API/Schema/Contexts/LibraryContext.cs b/API/Schema/Contexts/LibraryContext.cs index bbe9e3c..6473b5c 100644 --- a/API/Schema/Contexts/LibraryContext.cs +++ b/API/Schema/Contexts/LibraryContext.cs @@ -5,22 +5,10 @@ using Microsoft.EntityFrameworkCore.Diagnostics; namespace API.Schema.Contexts; -public class LibraryContext(DbContextOptions options) : DbContext(options) +public class LibraryContext(DbContextOptions options) : TrangaBaseContext(options) { public DbSet LibraryConnectors { get; set; } - private ILog Log => LogManager.GetLogger(GetType()); - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - base.OnConfiguring(optionsBuilder); - optionsBuilder.EnableSensitiveDataLogging(); - optionsBuilder.LogTo(s => - { - Log.Debug(s); - }, [DbLoggerCategory.Query.Name], LogLevel.Trace, DbContextLoggerOptions.Level | DbContextLoggerOptions.Category); - } - protected override void OnModelCreating(ModelBuilder modelBuilder) { //LibraryConnector Types diff --git a/API/Schema/Contexts/NotificationsContext.cs b/API/Schema/Contexts/NotificationsContext.cs index 8fe9454..05d6644 100644 --- a/API/Schema/Contexts/NotificationsContext.cs +++ b/API/Schema/Contexts/NotificationsContext.cs @@ -5,19 +5,8 @@ using Microsoft.EntityFrameworkCore.Diagnostics; namespace API.Schema.Contexts; -public class NotificationsContext(DbContextOptions options) : DbContext(options) +public class NotificationsContext(DbContextOptions options) : TrangaBaseContext(options) { public DbSet NotificationConnectors { get; set; } public DbSet Notifications { get; set; } - - private ILog Log => LogManager.GetLogger(GetType()); - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - base.OnConfiguring(optionsBuilder); - optionsBuilder.EnableSensitiveDataLogging(); - optionsBuilder.LogTo(s => - { - Log.Debug(s); - }, [DbLoggerCategory.Query.Name], LogLevel.Trace, DbContextLoggerOptions.Level | DbContextLoggerOptions.Category); - } } \ No newline at end of file diff --git a/API/Schema/Contexts/PgsqlContext.cs b/API/Schema/Contexts/PgsqlContext.cs index 6e3c1a6..3ab94e1 100644 --- a/API/Schema/Contexts/PgsqlContext.cs +++ b/API/Schema/Contexts/PgsqlContext.cs @@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Diagnostics; namespace API.Schema.Contexts; -public class PgsqlContext(DbContextOptions options) : DbContext(options) +public class PgsqlContext(DbContextOptions options) : TrangaBaseContext(options) { public DbSet Jobs { get; set; } public DbSet MangaConnectors { get; set; } @@ -19,17 +19,6 @@ public class PgsqlContext(DbContextOptions options) : DbContext(op public DbSet> MangaConnectorToManga { get; set; } public DbSet> MangaConnectorToChapter { get; set; } public DbSet MetadataEntries { get; set; } - private ILog Log => LogManager.GetLogger(GetType()); - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - base.OnConfiguring(optionsBuilder); - optionsBuilder.EnableSensitiveDataLogging(); - optionsBuilder.LogTo(s => - { - Log.Debug(s); - }, [DbLoggerCategory.Query.Name], LogLevel.Trace, DbContextLoggerOptions.Level | DbContextLoggerOptions.Category); - } protected override void OnModelCreating(ModelBuilder modelBuilder) { diff --git a/API/Schema/Contexts/TrangaBaseContext.cs b/API/Schema/Contexts/TrangaBaseContext.cs new file mode 100644 index 0000000..56a1598 --- /dev/null +++ b/API/Schema/Contexts/TrangaBaseContext.cs @@ -0,0 +1,19 @@ +using log4net; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; + +namespace API.Schema.Contexts; + +public abstract class TrangaBaseContext(DbContextOptions options) : DbContext(options) where T : DbContext +{ + private ILog Log => LogManager.GetLogger(GetType()); + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + optionsBuilder.LogTo(s => + { + Log.Debug(s); + }, Array.Empty(), LogLevel.Warning, DbContextLoggerOptions.Level | DbContextLoggerOptions.Category | DbContextLoggerOptions.UtcTime); + } +} \ No newline at end of file