SharpRSS/SharpRSS.Business/DependencyInjection.cs

86 lines
3.6 KiB
C#

using DotBased.Logging;
using DotBased.Logging.Serilog;
using DotBased.Logging.MEL;
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;
using AuthService = SharpRSS.Business.Services.AuthService;
namespace SharpRSS.Business;
public static class DependencyInjection
{
public static WebApplicationBuilder AddSRSS(this WebApplicationBuilder builder)
{
/*
* Logging
*/
LogService.Initialize(options =>
{
options.Severity = LogSeverity.Verbose;
options.AddSeverityFilter("Microsoft", LogSeverity.Info);
options.AddSeverityFilter("Microsoft.Hosting.Lifetime", LogSeverity.Info);
options.AddSeverityFilter("Microsoft.AspNetCore", LogSeverity.Warning);
options.AddSeverityFilter("Microsoft.AspNetCore.Authentication", LogSeverity.Info);
options.AddSeverityFilter("MudBlazor", LogSeverity.Info);
});
Log.Logger = new LoggerConfiguration().UseBasedExtension()
.MinimumLevel.Verbose()
.Enrich.WithProperty("Application", "SharpRSS")
.WriteTo.Console(outputTemplate: BasedSerilog.OutputTemplate)
.WriteTo.File(path: Path.Combine("Logs", "Debug", "log_.log"), rollingInterval: RollingInterval.Day, outputTemplate: BasedSerilog.OutputTemplate)
.Destructure.ToMaximumDepth(4)
.Destructure.ToMaximumStringLength(100)
.Destructure.ToMaximumCollectionCount(10).CreateLogger();
LogService.AddLogAdapter(new BasedSerilogAdapter(Log.Logger));
var _logger = LogService.RegisterLogger(typeof(DependencyInjection));
builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(LogLevel.Trace);
builder.Logging.AddDotBasedLoggerProvider(LogService.Options);
/*
* 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'");
}
});
/*
* Services
*/
builder.Services.AddScoped<AuthService>();
//TODO: Auth, Settings
return builder;
}
}