feat: 增加数据库日志任务

This commit is contained in:
Argo-Tianyi 2021-12-29 11:22:21 +08:00
parent 5a61625f6f
commit 3ef333f6ec
4 changed files with 94 additions and 5 deletions

View File

@ -0,0 +1,33 @@
using System.ComponentModel;
namespace BootstrapAdmin.DataAccess.Models
{
/// <summary>
/// 后台数据库脚本执行日志实体类
/// </summary>
public class DBLog
{
/// <summary>
/// 获得/设置 主键ID
/// </summary>
public string? Id { get; set; }
/// <summary>
/// 获得/设置 当前登陆名
/// </summary>
[DisplayName("所属用户")]
public string? UserName { get; set; }
/// <summary>
/// 获得/设置 数据库执行脚本
/// </summary>
[DisplayName("脚本内容")]
public string SQL { get; set; } = "";
/// <summary>
/// 获取/设置 用户角色关联状态 checked 标示已经关联 '' 标示未关联
/// </summary>
[DisplayName("执行时间")]
public DateTime LogTime { get; set; }
}
}

View File

@ -0,0 +1,59 @@
using BootstrapAdmin.DataAccess.Models;
using Longbow.Tasks;
using PetaPoco;
using System.Collections.Concurrent;
namespace BootstrapAdmin.Web.Jobs
{
/// <summary>
/// 数据库脚本执行日志任务实体类
/// </summary>
public class DBLogTask : ITask
{
private static readonly BlockingCollection<DBLog> _messageQueue = new(new ConcurrentQueue<DBLog>());
/// <summary>
/// 添加数据库日志实体类到内部集合中
/// </summary>
/// <param name="log"></param>
public static System.Threading.Tasks.Task AddDBLog(DBLog log) => System.Threading.Tasks.Task.Run(() =>
{
if (!_messageQueue.IsAddingCompleted && !_pause)
{
_messageQueue.Add(log);
}
});
private static bool _pause;
/// <summary>
/// 暂停接收脚本执行日志
/// </summary>
public static void Pause() => _pause = true;
/// <summary>
/// 开始接收脚本执行日志
/// </summary>
public static void Run() => _pause = false;
/// <summary>
/// 任务执行方法
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public System.Threading.Tasks.Task Execute(CancellationToken cancellationToken)
{
var logs = new List<DBLog>();
while (_messageQueue.TryTake(out var log))
{
if (log != null) logs.Add(log);
if (logs.Count >= 100) break;
}
if (logs.Any())
{
//using var db = DbManager.Create(enableLog: false);
//db.InsertBatch(logs);
}
return System.Threading.Tasks.Task.CompletedTask;
}
}
}

View File

@ -54,7 +54,7 @@ namespace Microsoft.Extensions.DependencyInjection
// 真实任务负责批次写入数据执行脚本到日志中
TaskServicesManager.GetOrAdd<DBLogTask>("SQL日志", TriggerBuilder.Build(Cron.Minutely()));
// 真实人物负责周期性设置健康检查结果开关为开启
// 真实任务负责周期性设置健康检查结果开关为开启
TaskServicesManager.GetOrAdd("健康检查", token => Task.FromResult(DictHelper.SaveSettings(new BootstrapDict[] {
new BootstrapDict() {
Category = "网站设置",

View File

@ -91,10 +91,7 @@ namespace Bootstrap.Admin
options.Filters.Add<SignalRExceptionFilter<SignalRHub>>();
}).AddJsonOptions(op => op.JsonSerializerOptions.AddDefaultConverters());
services.AddRazorPages();
services.AddServerSideBlazor().AddCircuitOptions(options =>
{
if (Enviroment.IsDevelopment()) options.DetailedErrors = true;
});
services.AddServerSideBlazor();
services.AddDisplayNames();
services.AddBootstrapBlazor();
}