using DotBased.Logging; using DotBased.Logging.MEL; using DotBased.Logging.Serilog; using Manager.App.Models.Settings; using Manager.App.Services; using Manager.App.Services.System; using Manager.Data.Contexts; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using Serilog; namespace Manager.App; public static class DependencyInjection { public static void ManagerSetup(this WebApplicationBuilder builder) { builder.Services.AddDbContextFactory((serviceProvider, options) => { var libSettings = serviceProvider.GetRequiredService>().Value; var logger = serviceProvider.GetRequiredService>(); var dbPath = Path.Combine(libSettings.Path, "Library.db"); logger.LogInformation("Setting library database to: {DbPath}", dbPath); options.UseSqlite($"Data Source={dbPath}"); }); builder.RegisterExtendedBackgroundServices(); builder.Services.AddScoped(); } public static void SetupSettings(this WebApplicationBuilder builder) { builder.Services.AddOptions() .Bind(builder.Configuration.GetSection("Library")) .ValidateDataAnnotations() .PostConfigure(settings => { settings.Path = settings.Path.Replace("{workdir}", Environment.CurrentDirectory, StringComparison.InvariantCultureIgnoreCase); }) .ValidateOnStart(); builder.Services.AddOptions() .Bind(builder.Configuration.GetSection("Downloads")) .ValidateDataAnnotations() .ValidateOnStart(); } public static void SetupLogging(this WebApplicationBuilder builder) { var isDevelopment = builder.Environment.IsDevelopment(); var logSeverity = isDevelopment ? LogSeverity.Debug : LogSeverity.Info; var severityFilters = new Dictionary(); var dotBasedLogSection = builder.Configuration.GetSection("DotBased:Logging"); if (dotBasedLogSection.Exists()) { logSeverity = dotBasedLogSection.GetValue("Severity"); severityFilters = dotBasedLogSection.GetSection("SeverityFilters").GetChildren() .ToDictionary( x => x.Key, x => x.Get() ); } LogService.Initialize(options => { options.Severity = logSeverity; foreach (var filter in severityFilters) { options.AddSeverityFilter(filter.Key, filter.Value); } }); Log.Logger = new LoggerConfiguration().UseBasedExtension() .MinimumLevel.Verbose() .Enrich.WithProperty("Application", "ImportUI") .WriteTo.Console(outputTemplate: BasedSerilog.OutputTemplate) .WriteTo.File(path: Path.Combine("Logs", $"{(isDevelopment ? "Debug" : "Release")}", "log_.log"), rollingInterval: RollingInterval.Day, outputTemplate: BasedSerilog.OutputTemplate) .Destructure.ToMaximumDepth(4) .Destructure.ToMaximumStringLength(100) .Destructure.ToMaximumCollectionCount(10).CreateLogger(); LogService.AddLogAdapter(new BasedSerilogAdapter(Log.Logger)); builder.Logging.ClearProviders(); builder.Logging.SetMinimumLevel(isDevelopment ? LogLevel.Trace : LogLevel.Information); builder.Logging.AddDotBasedLoggerProvider(LogService.Options); } private static void RegisterExtendedBackgroundServices(this WebApplicationBuilder builder) { var assembly = typeof(Program).Assembly; foreach (var exBgService in assembly.GetTypes() .Where(t => typeof(ExtendedBackgroundService).IsAssignableFrom(t) && t is { IsClass: true, IsAbstract: false })) { builder.Services.AddSingleton(exBgService); builder.Services.AddSingleton(typeof(ExtendedBackgroundService), sp => (ExtendedBackgroundService)sp.GetRequiredService(exBgService)); builder.Services.AddSingleton(sp => (IHostedService)sp.GetRequiredService(exBgService)); } builder.Services.AddSingleton(); } }