This commit is contained in:
owenchen committed 2026-08-06 13:15:56 +08:00
1 parent 7eb017d8ee
commit 873f65dbbe
48 files changed
+1391 -1236

No files matched your search

@@ -26,6 +26,7 @@ namespace CloudBuilder.Core.DatabaseAccessor
private readonly IServiceProvider _serviceProvider;
private readonly IConfiguration configuration;
private readonly IHttpContextAccessor httpContextAccessor;
private FileLog fileLog;
public MasterDbContext(DbContextOptions<MasterDbContext> options,
IEnumerable<IMasterRegistEntityType<ModelBuilder>> entityManagers,
@@ -38,6 +39,7 @@ namespace CloudBuilder.Core.DatabaseAccessor
_serviceProvider = serviceProvider;
this.configuration = configuration;
this.httpContextAccessor = httpContextAccessor;
fileLog = new FileLog(configuration);
}
public override int SaveChanges(bool acceptAllChangesOnSuccess)
@@ -78,7 +80,7 @@ namespace CloudBuilder.Core.DatabaseAccessor
}
foreach (var entity in entities)
{
{
if (!(entity.State == EntityState.Added || entity.State == EntityState.Modified)) continue;
// 获取所有实体有效属性,排除 [NotMapper] 属性
@@ -132,7 +134,7 @@ namespace CloudBuilder.Core.DatabaseAccessor
// 拼接 WHERE 条件:Id=1 AND TenantId=2
var whereClauses = pkProps
.Select(p => $"{p.Metadata.GetColumnName()?? p.Metadata.Name} = '{p.CurrentValue?.ToString()?.Replace("'", "''")}'");
.Select(p => $"{p.Metadata.GetColumnName() ?? p.Metadata.Name} = '{p.CurrentValue?.ToString()?.Replace("'", "''")}'");
string whereSql = string.Join(" AND ", whereClauses);
string sqlLog = "";
@@ -169,42 +171,10 @@ namespace CloudBuilder.Core.DatabaseAccessor
sqlLog = $"[{uid}][{now.ToString("yyyy-MM-dd HH:mm:ss")}]DELETE FROM {entityName} WHERE {whereSql}";
}
WriteToFile(sqlLog);
fileLog.WriteToFile(sqlLog);
}
}
private void WriteToFile(string log)
{
try
{
long MaxFileSizeBytes = (long)(configuration.GetValue<int>("Logging:MaxFileSizeMB") * 1024 * 1024);
string today = DateTime.Now.ToString("yyyyMMdd");
string basePath = configuration["Logging:LogFileDirectory"];
Directory.CreateDirectory(basePath);
string baseFileName = $"log-{today}";
string ext = ".log";
string finalPath = Path.Combine(basePath, $"{baseFileName}{ext}");
int index = 1;
// 循环找没超过大小的文件
while (File.Exists(finalPath) && new FileInfo(finalPath).Length >= MaxFileSizeBytes)
{
finalPath = Path.Combine(basePath, $"{baseFileName}-{index}{ext}");
index++;
}
// 写入(线程安全)
lock (this)
{
File.AppendAllText(finalPath, log + Environment.NewLine, Encoding.UTF8);
}
}
catch { }
}
protected override void OnModelCreating(ModelBuilder builder)
{
foreach (IMasterRegistEntityType<ModelBuilder> entityManager in _entityManagers)