FIx error `The logger is already frozen`

This commit is contained in:
lihanbo 2024-10-16 11:24:10 +08:00
parent 59e8845568
commit 19a612160e
1 changed files with 37 additions and 40 deletions

View File

@ -1,4 +1,6 @@
using System.Reflection;
using LFlow.Base.Interfaces;
using LFlow.Base.Utils;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
@ -56,7 +58,7 @@ public static class Program
});
});
builder.Host.UseSerilog();
builder.Host.UseSerilog(Log.Logger);
// init App.service
App.services = builder.Services;
var app = builder.Build();
@ -74,6 +76,13 @@ public static class Program
u.DocumentTitle = "LFlow";
});
}
// 在启动后调用Sqlsugar进行CodeFirst
// 挂载启动后事件
app.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationStarted.Register(() =>
{
Log.Logger.Information("ApplicationStarted");
CodeFirst.InitTable();
});
app.Run();
}
@ -87,18 +96,20 @@ public static class Program
ConnectionString = "DataSource=LFlow-dev.db",
IsAutoCloseConnection = true,
},
db =>
{
//单例参数配置,所有上下文生效
db.Aop.OnLogExecuting = (sql, pars) =>
{
//获取作IOC作用域对象
var appServive = s.GetService<IHttpContextAccessor>();
// var obj = appServive?.HttpContext?.RequestServices.GetService<Log>();
// Console.WriteLine("AOP" + obj.GetHashCode());
Console.WriteLine($"{appServive?.HttpContext?.Request.Path}:{sql}\r\n{pars}");
};
});
db =>
{
//单例参数配置,所有上下文生效
db.Aop.OnLogExecuting = (sql, pars) =>
{
//获取作IOC作用域对象
var appServive = s.GetService<IHttpContextAccessor>();
// var obj = appServive?.HttpContext?.RequestServices.GetService<Log>();
// Console.WriteLine("AOP" + obj.GetHashCode());
Console.WriteLine($"{appServive?.HttpContext?.Request.Path}:{sql}\r\n{pars}");
};
});
sqlSugar.DbMaintenance.CreateDatabase();
return sqlSugar;
});
}
@ -117,43 +128,27 @@ public static class Program
Log.Logger.Information($"Load file -> {file}...");
var assembly = Assembly.LoadFile(file);
var types = assembly.GetTypes();
bool isUseController = false;
// bool isUseController = false;
foreach (var type in types)
{
// Console.WriteLine(type);
if (type.IsClass && !type.IsAbstract)
{
var interfaces = type.GetInterfaces();
foreach (var inter in interfaces)
if (interfaces.Contains(typeof(IModule)))
{
if (inter.Name.Contains("IRepo"))
{
//注册数据仓库
services.AddScoped(inter, type);
}
if (inter.Name.Contains(type.Name))
{
//注册服务
services.AddScoped(inter, type);
}
if (inter.Name.Contains("IController"))
{
isUseController = true;
}
if (inter.Name.Equals("IDataModel"))
{
}
Log.Logger.Information($"\tFound IModule -> {type.FullName}");
var module = Activator.CreateInstance(type) as IModule;
module.ConfigureModule(services);
}
}
}
if (isUseController)
{
Log.Logger.Information($"\tAdd Controllers for {assembly.FullName}");
// 添加Controller
services.AddControllers().AddApplicationPart(assembly);
}
// if (isUseController)
// {
// Log.Logger.Information($"\tAdd Controllers for {assembly.FullName}");
// // 添加Controller
// services.AddControllers().AddApplicationPart(assembly);
// }
Log.Logger.Information("done.\r\n");
}
}
@ -164,4 +159,6 @@ public static class Program
}
}
}