ow
This commit is contained in:
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)
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
/// <summary>
|
||||
/// 文件日志写入封装类,自动按日期分文件、超大小自动切割
|
||||
/// </summary>
|
||||
public class FileLog
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly object _lockObj = new object(); // 独立锁,不依赖外部this
|
||||
|
||||
public FileLog(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入日志内容到文件
|
||||
/// </summary>
|
||||
/// <param name="log">日志文本</param>
|
||||
public void WriteToFile(string log)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 读取配置,转换最大文件字节数
|
||||
int maxMb = _configuration.GetValue<int>("Logging:MaxFileSizeMB");
|
||||
long maxFileSizeBytes = (long)maxMb * 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 (_lockObj)
|
||||
{
|
||||
File.AppendAllText(finalPath, log + Environment.NewLine, Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 原逻辑吞掉所有异常,如需日志可自行增加内部异常输出
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user