using FreeSql; using Microsoft.Extensions.DependencyInjection; using Znyc.CloudCar.Auth.HttpContextUser; using Znyc.CloudCar.Configuration; using Znyc.CloudCar.Core.Db; namespace Znyc.CloudCar.Core.Config { /// /// FreeSql /// public static class FreeSqlSetup { public static void AddFreeSqlSetup(this IServiceCollection services) { var freeSqlBuiler = new FreeSqlBuilder() .UseConnectionString(DataType.MySql, AppSettingsConstVars.DbSqlConnection) .UseLazyLoading(false) .UseNoneCommandParameter(true); //监听所有命令 if (AppSettingsConstVars.SqlMonitorCommand) { freeSqlBuiler.UseMonitorCommand(cmd => { }, (cmd, traceLog) => { Console.WriteLine($"{cmd.CommandText}\r\n"); }); } var fsql = freeSqlBuiler.Build(); services.AddScoped(); services.AddSingleton(fsql); //监听所有sql语句 if (AppSettingsConstVars.SqlCurd) { fsql.Aop.CurdAfter += (s, e) => { Console.WriteLine($"{e.Sql}\r\n"); Console.WriteLine($"耗时:{e.ElapsedMilliseconds}ms\r\n"); }; } //审计数据 var user = services.BuildServiceProvider().GetService(); fsql.Aop.AuditValue += (s, e) => { DbHelper.AuditValue(e, user); }; } } }