diff --git a/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs b/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs index 467c3c6..5ae97e4 100644 --- a/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs +++ b/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs @@ -7,8 +7,10 @@ using Infrastructure.Options; using Infrastructure.Repository; using Infrastructure.Repository.Mongo; using Infrastructure.Repository.Redis; +using Infrastructure.Seed; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using MongoDB.Driver; using Newtonsoft.Json; @@ -24,6 +26,29 @@ namespace Infrastructure.Extensions; public static class ServiceCollectionExtensions { + public static WebApplicationBuilder AddDefaultInfrastructure(this WebApplicationBuilder builder) + where T : IEquatable + { + ArgumentNullException.ThrowIfNull(builder); + var services = builder.Services; + var configuration = builder.Configuration; + services.AddDefaultUserContext() + .AddDefaultAesEncryption() + .AddDefaultAuthentication(configuration) + .AddDefaultAuthorize(configuration) + .AddDefaultControllers() + .AddDefaultCors(configuration) + .AddDefaultApiVersioning(configuration) + .AddDefaultRedis(configuration) + .AddDefaultSerilog(configuration) + .AddDefaultRepositoryContext() + .AddDefaultSqlSugar(configuration, builder.Environment) + .AddDefaultMongoDb(configuration) + .AddDefaultTokenContext() + .AddDefaultDatabaseSeed(); + return builder; + } + public static TOptions? GetOptions(this IConfiguration configuration, string sectionName) where TOptions : OptionsBase { @@ -222,7 +247,7 @@ public static class ServiceCollectionExtensions /// /// /// - public static IServiceCollection AddAesEncryption(this IServiceCollection services) + public static IServiceCollection AddDefaultAesEncryption(this IServiceCollection services) { ArgumentNullException.ThrowIfNull(services); services.TryAddSingleton(); @@ -235,7 +260,7 @@ public static class ServiceCollectionExtensions /// /// /// - public static IServiceCollection AddMongoDbSetup( + public static IServiceCollection AddDefaultMongoDb( this IServiceCollection services, IConfiguration configuration) { @@ -276,10 +301,13 @@ public static class ServiceCollectionExtensions /// /// /// - public static IServiceCollection AddDefaultSqlSugarSetup( + public static IServiceCollection AddDefaultSqlSugar( this IServiceCollection services, IConfiguration configuration, - IWebHostEnvironment hostEnvironment) + IWebHostEnvironment hostEnvironment, + Action? onDataChanging = null, + Action? onDiffLogEvent = null + ) { ArgumentNullException.ThrowIfNull(services); @@ -330,13 +358,20 @@ public static class ServiceCollectionExtensions } }; - var sugarScope = new SqlSugarScope(connectionConfig, config => + var sugarScope = new SqlSugarScope(connectionConfig, client => { - config.QueryFilter.AddTableFilter(d => !d.IsDeleted); + client.QueryFilter.AddTableFilter(d => !d.IsDeleted); if (hostEnvironment.IsDevelopment() || hostEnvironment.IsStaging()) { - config.Aop.OnLogExecuting = (sql, parameters) => { Log.Logger.Information(sql); }; + client.Aop.OnLogExecuted = (sql, parameters) => + { + var elapsed = client.Ado.SqlExecutionTime.TotalSeconds; + Log.Logger.Information("sql: {sql} elapsed: {time} seconds", sql, elapsed); + }; } + + client.Aop.DataExecuting = onDataChanging; + client.Aop.OnDiffLogEvent = onDiffLogEvent; }); services.AddSingleton(sugarScope); return services; @@ -466,6 +501,12 @@ public static class ServiceCollectionExtensions return services; } + /// + /// 配置Serilog + /// + /// + /// + /// public static IServiceCollection AddDefaultSerilog( this IServiceCollection services, IConfiguration configuration) @@ -481,8 +522,7 @@ public static class ServiceCollectionExtensions var loggerConfiguration = new LoggerConfiguration() .MinimumLevel.Information() .MinimumLevel.Override("Microsoft", LogEventLevel.Information) - .WriteTo.Console(theme: AnsiConsoleTheme.Code) - .Enrich.FromLogContext(); + .WriteTo.Console(theme: AnsiConsoleTheme.Code); if (serilogOptions.WriteFile) { @@ -507,4 +547,13 @@ public static class ServiceCollectionExtensions }); return services; } + + public static IServiceCollection AddDefaultDatabaseSeed(this IServiceCollection services) + { + ArgumentNullException.ThrowIfNull(services); + + services.TryAddScoped(); + services.TryAddScoped(); + return services; + } } \ No newline at end of file