Added logging & DBContextFactory implementation. Updated 'DotBased' Module

This commit is contained in:
Max 2024-06-16 21:09:49 +02:00
parent 1d75dd6da0
commit 5bb96a48ee
10 changed files with 192 additions and 12 deletions

@ -1 +1 @@
Subproject commit c1d18d5b47f3dec81c5c153b8a66daf921b3eeef
Subproject commit 03daea46e78dfa97d3e4f85fa8035fef5c5ed446

View File

@ -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<IDbContextFactory<SRSSContext>>();
if (contextFactory != null)
{
await using var context = await contextFactory.CreateDbContextAsync();
context.Database.EnsureCreated();
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{

BIN
SharpRSS.Blazor/SRSS.db Normal file

Binary file not shown.

View File

@ -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"
}
}
}

View File

@ -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": "*"

View File

@ -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<SRSSContext>(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;
}
}

View File

@ -13,4 +13,27 @@
<ProjectReference Include="..\SharpRSS.Data\SharpRSS.Data.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.AspNetCore">
<HintPath>..\..\..\..\..\usr\lib64\dotnet\shared\Microsoft.AspNetCore.App\8.0.4\Microsoft.AspNetCore.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Configuration.Abstractions">
<HintPath>..\..\..\..\..\usr\lib64\dotnet\shared\Microsoft.AspNetCore.App\8.0.4\Microsoft.Extensions.Configuration.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions">
<HintPath>..\..\..\..\..\usr\lib64\dotnet\shared\Microsoft.AspNetCore.App\8.0.4\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.6" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
</Project>

View File

@ -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;
}

View File

@ -1,9 +1,12 @@
using Microsoft.EntityFrameworkCore;
namespace SharpRSS.Data;
public class SRSSContext
public sealed class SRSSContext : DbContext
{
public SRSSContext()
public SRSSContext(DbContextOptions<SRSSContext> options) : base(options)
{
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
ChangeTracker.LazyLoadingEnabled = false;
}
}

View File

@ -10,4 +10,8 @@
<ProjectReference Include="..\SharpRSS.Core\SharpRSS.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
</ItemGroup>
</Project>