直接去百度网盘获取:链接:
https://pan.baidu.com/s/105JxRaqZcTGIrT365BzRRw
提取码:wzkm
使用的是.netCore 6所以在Program中的配置如下:
//注册上下文:AOP里面可以获取IOC对象,如果有现成框架比如Furion可以不写这一行
builder.Services.AddHttpContextAccessor();
//注册仓储
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
//注册SqlSugar
builder.Services.AddSingleton<ISqlSugarClient>(s =>
{
SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
{
DbType = DbType.MySql,
ConnectionString = builder.Configuration.GetConnectionString("MySqlString"),
IsAutoCloseConnection = true,
},
db =>
{
//单例参数配置,所有上下文生效
db.Aop.OnLogExecuting = (sql, pars) =>
{
//获取IOC对象不要求在一个上下文
var log = s.GetService<ILogger<Program>>();
log.LogInformation(sql);
//获取IOC对象要求在一个上下文
//var appServive = s.GetService<IHttpContextAccessor>();
//var log= appServive?.HttpContext?.RequestServices.GetService<Log>();
};
});
return sqlSugar;
});
//日志配置
#if !DEBUG
//替换默认日志
builder.WebHost.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
})
.UseNLog();
#endif
//ServiceProviderInstance设置
ServiceProviderInstance.Instance = app.Services;
标签:logging,log,builder,仓储,Services,上下文,SqlSugar,搭建 From: https://www.cnblogs.com/zkmblog/p/17572441.html