[Blazor] Use settings from appsettings.json to configure folders and log levels.

This commit is contained in:
2025-07-27 18:19:01 +01:00
parent b62f495824
commit ab804239bb
9 changed files with 130 additions and 102 deletions

View File

@@ -1,54 +1,55 @@
<Project ToolsVersion="15.0">
<ItemGroup>
<PackageVersion Include="Aaru.Checksums.Native" Version="6.0.0-alpha.10" />
<PackageVersion Include="AsyncFixer" Version="1.6.0" />
<PackageVersion Include="Avalonia.Controls.DataGrid" Version="11.3.2" />
<PackageVersion Include="Avalonia.Desktop" Version="11.3.2" />
<PackageVersion Include="Avalonia.Diagnostics" Version="11.3.2" />
<PackageVersion Include="Avalonia.Svg.Skia" Version="11.3.0" />
<PackageVersion Include="Avalonia.Themes.Fluent" Version="11.3.2" />
<PackageVersion Include="Avalonia" Version="11.3.2" />
<PackageVersion Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageVersion Include="DotNetZip" Version="1.16.0" />
<PackageVersion Include="EFCore.BulkExtensions" Version="9.0.1" />
<PackageVersion Include="ErrorProne.NET.CoreAnalyzers" Version="0.1.2" />
<PackageVersion Include="ErrorProne.NET.Structs" Version="0.1.2" />
<PackageVersion Include="InclusivenessAnalyzer" Version="1.3.0" />
<PackageVersion Include="MessageBox.Avalonia" Version="3.2.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.7" />
<PackageVersion Include="Microsoft.Extensions.Localization" Version="9.0.7" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.7" />
<PackageVersion Include="Microsoft.FluentUI.AspNetCore.Components.DataGrid.EntityFrameworkAdapter" Version="4.12.1" />
<PackageVersion Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.14.15" />
<PackageVersion Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageVersion Include="Mono.Fuse.NETStandard" Version="1.1.0" />
<PackageVersion Include="Philips.CodeAnalysis.MaintainabilityAnalyzers" Version="1.6.3" />
<PackageVersion Include="plist-cil" Version="2.2.0" />
<PackageVersion Include="Roslynator.Analyzers" Version="4.14.0" />
<PackageVersion Include="Roslynator.CodeAnalysis.Analyzers" Version="4.14.0" />
<PackageVersion Include="Roslynator.Formatting.Analyzers" Version="4.14.0" />
<PackageVersion Include="SabreTools.Models" Version="1.5.8" />
<PackageVersion Include="Serilog" Version="4.3.0" />
<PackageVersion Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="9.0.2" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="SharpCompress" Version="0.39.0" />
<PackageVersion Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" />
<PackageVersion Include="System.Security.Principal.Windows" Version="5.0.0" />
<PackageVersion Include="Text.Analyzers" Version="4.14.0" />
<PackageVersion Include="winfsp.net" Version="2.1.25156" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="SabreTools.Hashing" Version="1.2.3" />
<PackageVersion Include="SabreTools.IO" Version="1.4.13" />
<PackageVersion Include="SabreTools.Matching" Version="1.3.4" />
<PackageVersion Include="SabreTools.Skippers" Version="1.1.3" />
<PackageVersion Include="System.IO.Compression" Version="4.3.0" />
<PackageVersion Include="SharpCompress" Version="0.38.0" />
<PackageVersion Include="System.Text.Encoding.CodePages" Version="8.0.0" />
<PackageVersion Include="ZstdSharp.Port" Version="0.8.6" />
<PackageVersion Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.12.1" />
<PackageVersion Include="Microsoft.FluentUI.AspNetCore.Components.Icons" Version="4.12.1" />
<PackageVersion Include="Aaru.Checksums.Native" Version="6.0.0-alpha.10"/>
<PackageVersion Include="AsyncFixer" Version="1.6.0"/>
<PackageVersion Include="Avalonia.Controls.DataGrid" Version="11.3.2"/>
<PackageVersion Include="Avalonia.Desktop" Version="11.3.2"/>
<PackageVersion Include="Avalonia.Diagnostics" Version="11.3.2"/>
<PackageVersion Include="Avalonia.Svg.Skia" Version="11.3.0"/>
<PackageVersion Include="Avalonia.Themes.Fluent" Version="11.3.2"/>
<PackageVersion Include="Avalonia" Version="11.3.2"/>
<PackageVersion Include="CommunityToolkit.Mvvm" Version="8.4.0"/>
<PackageVersion Include="DotNetZip" Version="1.16.0"/>
<PackageVersion Include="EFCore.BulkExtensions" Version="9.0.1"/>
<PackageVersion Include="ErrorProne.NET.CoreAnalyzers" Version="0.1.2"/>
<PackageVersion Include="ErrorProne.NET.Structs" Version="0.1.2"/>
<PackageVersion Include="InclusivenessAnalyzer" Version="1.3.0"/>
<PackageVersion Include="MessageBox.Avalonia" Version="3.2.0"/>
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.7"/>
<PackageVersion Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.7"/>
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.7"/>
<PackageVersion Include="Microsoft.Extensions.Localization" Version="9.0.7"/>
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.7"/>
<PackageVersion Include="Microsoft.FluentUI.AspNetCore.Components.DataGrid.EntityFrameworkAdapter" Version="4.12.1"/>
<PackageVersion Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.14.15"/>
<PackageVersion Include="Microsoft.Win32.Registry" Version="5.0.0"/>
<PackageVersion Include="Mono.Fuse.NETStandard" Version="1.1.0"/>
<PackageVersion Include="Philips.CodeAnalysis.MaintainabilityAnalyzers" Version="1.6.3"/>
<PackageVersion Include="plist-cil" Version="2.2.0"/>
<PackageVersion Include="Roslynator.Analyzers" Version="4.14.0"/>
<PackageVersion Include="Roslynator.CodeAnalysis.Analyzers" Version="4.14.0"/>
<PackageVersion Include="Roslynator.Formatting.Analyzers" Version="4.14.0"/>
<PackageVersion Include="SabreTools.Models" Version="1.5.8"/>
<PackageVersion Include="Serilog" Version="4.3.0"/>
<PackageVersion Include="Serilog.AspNetCore" Version="9.0.0"/>
<PackageVersion Include="Serilog.Extensions.Logging" Version="9.0.2"/>
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0"/>
<PackageVersion Include="Serilog.Sinks.File" Version="7.0.0"/>
<PackageVersion Include="SharpCompress" Version="0.39.0"/>
<PackageVersion Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31"/>
<PackageVersion Include="System.Security.Principal.Windows" Version="5.0.0"/>
<PackageVersion Include="Text.Analyzers" Version="4.14.0"/>
<PackageVersion Include="winfsp.net" Version="2.1.25156"/>
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageVersion Include="SabreTools.Hashing" Version="1.2.3"/>
<PackageVersion Include="SabreTools.IO" Version="1.4.13"/>
<PackageVersion Include="SabreTools.Matching" Version="1.3.4"/>
<PackageVersion Include="SabreTools.Skippers" Version="1.1.3"/>
<PackageVersion Include="System.IO.Compression" Version="4.3.0"/>
<PackageVersion Include="SharpCompress" Version="0.38.0"/>
<PackageVersion Include="System.Text.Encoding.CodePages" Version="8.0.0"/>
<PackageVersion Include="ZstdSharp.Port" Version="0.8.6"/>
<PackageVersion Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.12.1"/>
<PackageVersion Include="Microsoft.FluentUI.AspNetCore.Components.Icons" Version="4.12.1"/>
</ItemGroup>
</Project>

View File

@@ -1,8 +1,8 @@
@using Microsoft.Extensions.Localization
@using RomRepoMgr.Blazor.Resources
@implements IDialogContentComponent
@inject ILogger<ImportDats> Logger
@inject IStringLocalizer<Localization> Localizer
@inject IConfiguration Configuration
<FluentDialog Width="800px" Height="400px" Title="Import DATs" Modal="true" TrapFocus="true">
<FluentDialogBody>

View File

@@ -1,6 +1,5 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components;
using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Workers;
using Serilog;
@@ -37,7 +36,7 @@ public partial class ImportDats : ComponentBase
{
base.OnInitialized();
path = Path.Combine(Environment.CurrentDirectory, Consts.IncomingDatFolder);
path = Configuration["DataFolders:ImportDats"] ?? "incoming-dats";
StatusMessage = "";
StatusMessage2 = "";
IsBusy = false;

View File

@@ -1,10 +1,10 @@
@using Microsoft.Extensions.Localization
@using RomRepoMgr.Blazor.Resources
@using RomRepoMgr.Database
@implements IDialogContentComponent
@inject ILogger<ImportRoms> Logger
@inject Context _ctx
@inject IStringLocalizer<Localization> Localizer
@inject IConfiguration Configuration
<FluentDialog Width="1600px" Height="800px" Title="Import DATs" Modal="true" TrapFocus="true">
<FluentDialogBody>

View File

@@ -1,7 +1,6 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using Microsoft.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components;
using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Workers;
using RomRepoMgr.Database.Models;
@@ -45,7 +44,7 @@ public partial class ImportRoms : ComponentBase
protected override void OnInitialized()
{
base.OnInitialized();
FolderPath = Path.Combine(Environment.CurrentDirectory, Consts.IncomingRomsFolder);
FolderPath = Configuration["DataFolders:ImportRoms"] ?? "incoming";
IsBusy = false;
NotYetStarted = true;
RemoveFilesChecked = false;

View File

@@ -1,11 +0,0 @@
namespace RomRepoMgr.Blazor;
public class Consts
{
public const string DbFolder = "db";
public const string DatFolder = "dats";
public const string IncomingDatFolder = "incoming-dats";
public const string RepositoryFolder = "repo";
public const string IncomingRomsFolder = "incoming";
public const string TemporaryFolder = "tmp";
}

View File

@@ -1,12 +1,40 @@
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using Microsoft.FluentUI.AspNetCore.Components;
using RomRepoMgr.Blazor;
using RomRepoMgr.Blazor.Components;
using RomRepoMgr.Database;
using RomRepoMgr.Settings;
using Serilog;
using Serilog.Events;
// Start the application
Log.Logger = new LoggerConfiguration().MinimumLevel.Debug().WriteTo.Console().Enrich.FromLogContext().CreateLogger();
Log.Information("Welcome to ROM Repository Manager!");
Log.Information("Copyright © 2020-2025 Natalia Portillo");
// Configuration and settings
// We need the builder now
Log.Debug("Creating the builder...");
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Access the full configuration
Log.Debug("Creating the configuration reader...");
ConfigurationManager config = builder.Configuration;
string logFile = config["LogFile"] ?? "logs/rom-repo-mgr.log";
string defaultLogLevel = config["Logging:LogLevel:Default"] ?? "Information";
// Parse to LogEventLevel
if(!Enum.TryParse(defaultLogLevel, true, out LogEventLevel level))
{
// Fallback if parsing fails
#if DEBUG
level = LogEventLevel.Debug;
#else
level = LogEventLevel.Information;
#endif
}
// Now create a logger with the specified log level and log file
Log.Logger = new LoggerConfiguration()
#if DEBUG
.MinimumLevel.Debug()
@@ -14,23 +42,37 @@ Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
#endif
.WriteTo.Console()
.WriteTo.File(logFile, rollingInterval: RollingInterval.Day, fileSizeLimitBytes: 10 * 1048576)
.Enrich.FromLogContext()
.CreateLogger();
Log.Information("Welcome to ROM Repository Manager!");
Log.Information("Copyright © 2020-2025 Natalia Portillo");
// Read the rest of the configuration and settings
Log.Debug("Reading configuration settings...");
string repoFolder = config["DataFolders:Repository"] ?? "repo";
string importRoms = config["DataFolders:ImportRoms"] ?? "incoming";
string importDats = config["DataFolders:ImportDats"] ?? "incoming-dats";
string exportRoms = config["DataFolders:ExportRoms"] ?? "export";
string exportDats = config["DataFolders:ExportDats"] ?? "export-dats";
string databaseFolder = config["DataFolders:Database"] ?? "db";
string temporaryFolder = config["DataFolders:Temporary"] ?? "tmp";
string compressionTypeString = config["CompressionType"] ?? "Zstd";
// Parse the compression type
if(!Enum.TryParse(compressionTypeString, true, out CompressionType compressionType))
{
// Fallback if parsing fails
compressionType = CompressionType.Zstd;
}
// Ensure the folders exist
Log.Information("Ensuring folders exist...");
string[] folders =
[
Consts.DbFolder, Consts.DatFolder, Consts.IncomingDatFolder, Consts.RepositoryFolder, Consts.IncomingRomsFolder
];
string[] folders = [repoFolder, importRoms, importDats, databaseFolder, exportRoms, exportDats, temporaryFolder];
foreach(string folder in folders)
{
if(!Directory.Exists(folder))
// Check File.Exists for symlinks or junctions
if(!Directory.Exists(folder) && !File.Exists(folder))
{
Log.Debug("Creating folder: {Folder}", folder);
Directory.CreateDirectory(folder);
@@ -39,19 +81,8 @@ foreach(string folder in folders)
Log.Debug("Folder already exists: {Folder}", folder);
}
// Ensure the temporary folder exists but it can also be a symlink
if(!Directory.Exists(Consts.TemporaryFolder) && !File.Exists(Consts.TemporaryFolder))
{
Log.Debug("Creating folder: {TemporaryFolder}", Consts.TemporaryFolder);
Directory.CreateDirectory(Consts.TemporaryFolder);
}
else
Log.Debug("Folder already exists: {TemporaryFolder}", Consts.TemporaryFolder);
Log.Debug("Creating the builder...");
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog(); // ✅ Plug Serilog into the host
// ✅ Plug Serilog into the host
builder.Host.UseSerilog();
// Add services to the container.
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
@@ -64,7 +95,7 @@ Log.Debug("Creating database context...");
builder.Services.AddDbContextFactory<Context>(options =>
{
options.UseSqlite($"Data Source={Consts.DbFolder}/database.db");
options.UseSqlite($"Data Source={databaseFolder}/database.db");
#if DEBUG
options.EnableSensitiveDataLogging();
options.LogTo(Log.Debug);
@@ -79,11 +110,11 @@ Log.Debug("Setting the settings...");
Settings.Current = new SetSettings
{
DatabasePath = Path.Combine(Environment.CurrentDirectory, Consts.DbFolder, "database.db"),
TemporaryFolder = Path.Combine(Environment.CurrentDirectory, Consts.TemporaryFolder),
RepositoryPath = Path.Combine(Environment.CurrentDirectory, Consts.RepositoryFolder),
DatabasePath = Path.Combine(Environment.CurrentDirectory, databaseFolder, "database.db"),
TemporaryFolder = Path.Combine(Environment.CurrentDirectory, temporaryFolder),
RepositoryPath = Path.Combine(Environment.CurrentDirectory, repoFolder),
UseInternalDecompressor = true,
Compression = CompressionType.Zstd // Todo: Read from configuration
Compression = compressionType
};
Log.Debug("Building the application...");
@@ -104,10 +135,7 @@ app.MapStaticAssets();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
// Localization
string[] supportedCultures =
[
"en", "es"
];
string[] supportedCultures = ["en", "es"];
RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions().SetDefaultCulture("en")
.AddSupportedCultures(supportedCultures)

View File

@@ -20,6 +20,7 @@
<PackageReference Include="Serilog"/>
<PackageReference Include="Serilog.AspNetCore"/>
<PackageReference Include="Serilog.Sinks.Console"/>
<PackageReference Include="Serilog.Sinks.File"/>
</ItemGroup>
<ItemGroup>

View File

@@ -1,9 +1,20 @@
{
"Logging": {
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
"LogFile": "logs/romrepomgr.log",
"AllowedHosts": "*",
"DataFolders": {
"Repository": "repo",
"ImportRoms": "incoming",
"ImportDats": "incoming-dats",
"ExportRoms": "export",
"ExportDats": "export-dats",
"Database": "db",
"Temporary": "tmp"
},
"CompressionType": "Zstd"
}