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