From 5bb96a48eeebc5bb8ec1edfa0fc76daf6e619282 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 16 Jun 2024 21:09:49 +0200 Subject: [PATCH] Added logging & DBContextFactory implementation. Updated 'DotBased' Module --- DotBased | 2 +- SharpRSS.Blazor/Program.cs | 12 ++++ SharpRSS.Blazor/SRSS.db | Bin 0 -> 4096 bytes SharpRSS.Blazor/appsettings.Development.json | 46 +++++++++++++-- SharpRSS.Blazor/appsettings.json | 44 +++++++++++++- SharpRSS.Business/DependencyInjection.cs | 55 +++++++++++++++++- SharpRSS.Business/SharpRSS.Business.csproj | 23 ++++++++ .../Configuration/DatabaseSettings.cs | 9 +++ SharpRSS.Data/SRSSContext.cs | 9 ++- SharpRSS.Data/SharpRSS.Data.csproj | 4 ++ 10 files changed, 192 insertions(+), 12 deletions(-) create mode 100644 SharpRSS.Blazor/SRSS.db create mode 100644 SharpRSS.Core/Configuration/DatabaseSettings.cs diff --git a/DotBased b/DotBased index c1d18d5..03daea4 160000 --- a/DotBased +++ b/DotBased @@ -1 +1 @@ -Subproject commit c1d18d5b47f3dec81c5c153b8a66daf921b3eeef +Subproject commit 03daea46e78dfa97d3e4f85fa8035fef5c5ed446 diff --git a/SharpRSS.Blazor/Program.cs b/SharpRSS.Blazor/Program.cs index 3e68ef7..082ee18 100644 --- a/SharpRSS.Blazor/Program.cs +++ b/SharpRSS.Blazor/Program.cs @@ -1,13 +1,25 @@ +using Microsoft.EntityFrameworkCore; using SharpRSS.Blazor.Components; +using SharpRSS.Business; +using SharpRSS.Data; var builder = WebApplication.CreateBuilder(args); +builder.UseSRSS(); + // Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); var app = builder.Build(); +var contextFactory = app.Services.GetService>(); +if (contextFactory != null) +{ + await using var context = await contextFactory.CreateDbContextAsync(); + context.Database.EnsureCreated(); +} + // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { diff --git a/SharpRSS.Blazor/SRSS.db b/SharpRSS.Blazor/SRSS.db new file mode 100644 index 0000000000000000000000000000000000000000..9a472209435d88229f65ab7a0ca30d67df87a462 GIT binary patch literal 4096 zcmWFz^vNtqRY=P(%1ta$FlG>7U}9o$P*7lCU|@t|AVoG{WY9}{#S79dK(-m98b?E5 nGz3ONU^E0qLtr!nMnhmU1V%$(Gz3ONU^E0qLtr!nC=3ArNJIx= literal 0 HcmV?d00001 diff --git a/SharpRSS.Blazor/appsettings.Development.json b/SharpRSS.Blazor/appsettings.Development.json index 0c208ae..eedf5a9 100644 --- a/SharpRSS.Blazor/appsettings.Development.json +++ b/SharpRSS.Blazor/appsettings.Development.json @@ -1,8 +1,46 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Database": + { + "Server": "SQLite", + "Connection": "Data Source=SRSS.db" + }, + "Serilog": { + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], + "MinimumLevel": { + "Default": "Debug", + "Override": { + "System": "Information", + "Microsoft": "Debug", + "Microsoft.Hosting.Lifetime": "Debug", + "Microsoft.AspNetCore": "Debug", + "Microsoft.AspNetCore.Authentication": "Debug" + } + }, + "WriteTo": [ + { + "Name": "Console", + "Args": + { + "outputTemplate": "[{Timestamp:HH:mm:ss} - {Caller}->{Assembly}] | {Level:u3}] {Message:lj}{NewLine}{Exception}" + } + }, + { + "Name": "File", + "Args": + { + "path": "Logs/Debug/log_.log", + "RollingInterval": "Day", + "outputTemplate": "[{Timestamp:HH:mm:ss} - {Caller}->{Assembly}] | {Level:u3}] {Message:lj}{NewLine}{Exception}" + } + } + ], + "Destructure": [ + { "Name": "ToMaximumDepth", "Args": { "maximumDestructuringDepth": 4 } }, + { "Name": "ToMaximumStringLength", "Args": { "maximumStringLength": 100 } }, + { "Name": "ToMaximumCollectionCount", "Args": { "maximumCollectionCount": 10 } } + ], + "Properties": { + "Application": "SharpRSS" } } } diff --git a/SharpRSS.Blazor/appsettings.json b/SharpRSS.Blazor/appsettings.json index 10f68b8..fcc8986 100644 --- a/SharpRSS.Blazor/appsettings.json +++ b/SharpRSS.Blazor/appsettings.json @@ -1,8 +1,46 @@ { - "Logging": { - "LogLevel": { + "Database": + { + "Server": "SQLite", + "Connection": "Data Source=SRSS.db" + }, + "Serilog": { + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], + "MinimumLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Override": { + "System": "Information", + "Microsoft": "Information", + "Microsoft.Hosting.Lifetime": "Information", + "Microsoft.AspNetCore": "Warning", + "Microsoft.AspNetCore.Authentication": "Information" + } + }, + "WriteTo": [ + { + "Name": "Console", + "Args": + { + "outputTemplate": "[{Timestamp:HH:mm:ss} - {Caller}->{Assembly}] | {Level:u3}] {Message:lj}{NewLine}{Exception}" + } + }, + { + "Name": "File", + "Args": + { + "path": "Logs/Debug/log_.log", + "RollingInterval": "Day", + "outputTemplate": "[{Timestamp:HH:mm:ss} - {Caller}->{Assembly}] | {Level:u3}] {Message:lj}{NewLine}{Exception}" + } + } + ], + "Destructure": [ + { "Name": "ToMaximumDepth", "Args": { "maximumDestructuringDepth": 4 } }, + { "Name": "ToMaximumStringLength", "Args": { "maximumStringLength": 100 } }, + { "Name": "ToMaximumCollectionCount", "Args": { "maximumCollectionCount": 10 } } + ], + "Properties": { + "Application": "SharpRSS" } }, "AllowedHosts": "*" diff --git a/SharpRSS.Business/DependencyInjection.cs b/SharpRSS.Business/DependencyInjection.cs index 33f2e55..c0dc4bc 100644 --- a/SharpRSS.Business/DependencyInjection.cs +++ b/SharpRSS.Business/DependencyInjection.cs @@ -1,6 +1,59 @@ +using DotBased.Logging; +using DotBased.Logging.Serilog; +using Microsoft.AspNetCore.Builder; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.EntityFrameworkCore.Diagnostics; +using Serilog; +using SharpRSS.Core.Configuration; +using SharpRSS.Data; + namespace SharpRSS.Business; public static class DependencyInjection { - + public static WebApplicationBuilder UseSRSS(this WebApplicationBuilder builder) + { + // Logging (serilog) + var serilogConfig = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).UseBasedExtension(); + Log.Logger = serilogConfig.CreateLogger(); + + LogService.AddLogAdapter(new BasedSerilogAdapter(Log.Logger)); + var logger = LogService.RegisterLogger(typeof(DependencyInjection)); + + builder.Logging.ClearProviders(); + builder.Logging.AddSerilog(); + + // EF Core DbContextFactory + builder.Services.AddDbContextFactory(options => + { + var dbSettings = new DatabaseSettings(); + builder.Configuration.GetSection(DatabaseSettings.Configuration).Bind(dbSettings); + options.ConfigureWarnings(c => c.Log((RelationalEventId.CommandExecuted, LogLevel.Debug))); + switch (dbSettings.Server.ToUpper()) + { + case "SQLITE": + logger.Information("Configuring SQLite context..."); + options.UseSqlite(dbSettings.Connection); + break; + case "MARIADB": + logger.Information("Configuring MariaDB context..."); + var srvVersion = ServerVersion.AutoDetect(dbSettings.Connection); + logger.Information("Server found, version: {SrvVersion}", srvVersion.Version.Build); + options.UseMySql(dbSettings.Connection, srvVersion); + break; + case "MSSQL": + logger.Information("Configuring MSSQL context..."); + options.UseSqlServer(dbSettings.Connection); + break; + default: + throw new ArgumentException("No database server type specified in settings!, supported types: 'SQLite' 'MariaDB' 'MSSQL'"); + } + }); + + //TODO: Services, Auth, Settings + return builder; + } } \ No newline at end of file diff --git a/SharpRSS.Business/SharpRSS.Business.csproj b/SharpRSS.Business/SharpRSS.Business.csproj index 88bb0ef..5ac63b4 100644 --- a/SharpRSS.Business/SharpRSS.Business.csproj +++ b/SharpRSS.Business/SharpRSS.Business.csproj @@ -13,4 +13,27 @@ + + + ..\..\..\..\..\usr\lib64\dotnet\shared\Microsoft.AspNetCore.App\8.0.4\Microsoft.AspNetCore.dll + + + ..\..\..\..\..\usr\lib64\dotnet\shared\Microsoft.AspNetCore.App\8.0.4\Microsoft.Extensions.Configuration.Abstractions.dll + + + ..\..\..\..\..\usr\lib64\dotnet\shared\Microsoft.AspNetCore.App\8.0.4\Microsoft.Extensions.DependencyInjection.Abstractions.dll + + + + + + + + + + + + + + diff --git a/SharpRSS.Core/Configuration/DatabaseSettings.cs b/SharpRSS.Core/Configuration/DatabaseSettings.cs new file mode 100644 index 0000000..0950086 --- /dev/null +++ b/SharpRSS.Core/Configuration/DatabaseSettings.cs @@ -0,0 +1,9 @@ +namespace SharpRSS.Core.Configuration; + +public class DatabaseSettings +{ + public const string Configuration = "Database"; + + public string Server { get; set; } = string.Empty; + public string Connection { get; set; } = string.Empty; +} \ No newline at end of file diff --git a/SharpRSS.Data/SRSSContext.cs b/SharpRSS.Data/SRSSContext.cs index 9cc54cf..5222569 100644 --- a/SharpRSS.Data/SRSSContext.cs +++ b/SharpRSS.Data/SRSSContext.cs @@ -1,9 +1,12 @@ +using Microsoft.EntityFrameworkCore; + namespace SharpRSS.Data; -public class SRSSContext +public sealed class SRSSContext : DbContext { - public SRSSContext() + public SRSSContext(DbContextOptions options) : base(options) { - + ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; + ChangeTracker.LazyLoadingEnabled = false; } } \ No newline at end of file diff --git a/SharpRSS.Data/SharpRSS.Data.csproj b/SharpRSS.Data/SharpRSS.Data.csproj index cdd763f..6a9b5fb 100644 --- a/SharpRSS.Data/SharpRSS.Data.csproj +++ b/SharpRSS.Data/SharpRSS.Data.csproj @@ -10,4 +10,8 @@ + + + +