54 lines
1.3 KiB
C#
54 lines
1.3 KiB
C#
namespace CloudGaming.Shared.Admin.MessageHandler;
|
|
|
|
/// <summary>
|
|
/// 消费日志
|
|
/// </summary>
|
|
public class WriteLogHandler : IBus<SysOperationLog>
|
|
{
|
|
public string topic => "WriteInLog";
|
|
|
|
private readonly static ConcurrentBag<SysOperationLog> InsertList = [];
|
|
private readonly static object _lock = new();
|
|
|
|
private readonly IRepository<SysOperationLog> _sysOperationLogRepository;
|
|
|
|
public WriteLogHandler(IRepository<SysOperationLog> sysOperationLogRepository)
|
|
{
|
|
_sysOperationLogRepository = sysOperationLogRepository;
|
|
}
|
|
|
|
public async Task HandlerAsync(SysOperationLog data)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (InsertList.Count > 200)
|
|
{
|
|
_sysOperationLogRepository.InsertRange(InsertList);
|
|
InsertList.Clear();
|
|
}
|
|
|
|
InsertList.Add(data);
|
|
}
|
|
|
|
await Task.CompletedTask;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 10分钟检测一次 InsertList
|
|
/// </summary>
|
|
[Scheduled("0 0/2 * * * ?", remark: "2分钟检测一次 InsertList")]
|
|
public string Run()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (InsertList.Count > 0)
|
|
{
|
|
_sysOperationLogRepository.InsertRange(InsertList);
|
|
}
|
|
|
|
InsertList.Clear();
|
|
}
|
|
|
|
return "ok";
|
|
}
|
|
} |