添加日志相关业务类

This commit is contained in:
huangxianguo 2024-07-08 13:53:40 +08:00
parent a4d411bcb7
commit 35033aa980
6 changed files with 159 additions and 0 deletions

View File

@ -8,7 +8,10 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" /> <PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.15" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/> <PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="Quartz" Version="3.10.0" />
<PackageReference Include="sqlite-net-sqlcipher" Version="1.9.172" />
<PackageReference Include="System.Management" Version="8.0.0"/> <PackageReference Include="System.Management" Version="8.0.0"/>
<PackageReference Include="Serilog" Version="4.0.0"/> <PackageReference Include="Serilog" Version="4.0.0"/>
<PackageReference Include="Serilog.Sinks.File" Version="5.0.1-dev-00972"/> <PackageReference Include="Serilog.Sinks.File" Version="5.0.1-dev-00972"/>
@ -36,4 +39,8 @@
<None Remove=".gitignore" /> <None Remove=".gitignore" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Service\" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,15 @@
using SQLite;
namespace MasstransferCommon.Model.Entity;
/// <summary>
/// 数据库实体的父类
/// </summary>
public class Entity
{
[PrimaryKey] public string? Id { get; set; }
[Column("create_time")] public DateTime CreateTime { get; set; } = DateTime.Now;
[Column("update_time")] public DateTime UpdateTime { get; set; } = DateTime.Now;
}

View File

@ -0,0 +1,23 @@
using System.ComponentModel;
using SQLite;
namespace MasstransferCommon.Model.Entity;
/// <summary>
/// 日志参数
/// </summary>
[Table("log_params")]
[Description("日志参数")]
public class LogParams : Entity
{
[Column("level"), Description("日志级别")] public string? Level { get; set; }
[Column("path"), Description("日志存放路径")]
public string? Path { get; set; }
[Column("upload_corn"), Description("日志上传时间")]
public string? UploadCorn { get; set; }
[Column("upload_levels"), Description("日志上传级别")]
public string? UploadLevels { get; set; }
}

View File

@ -0,0 +1,98 @@
using Quartz;
using Quartz.Impl;
using Serilog;
namespace MasstransferCommon.scheduler;
public class SchedulerHelper
{
private static readonly Lazy<IScheduler> lazyScheduler = new(() => InitSchedulerAsync().GetAwaiter().GetResult());
private static readonly Dictionary<string, IJobDetail> _jobDetails = new();
private static IScheduler Scheduler => lazyScheduler.Value;
private static async Task<IScheduler> InitSchedulerAsync()
{
try
{
return await new StdSchedulerFactory().GetScheduler();
}
catch (Exception ex)
{
Log.Error($"Failed to initialize scheduler: {ex.Message}");
throw;
}
}
public static async Task Start()
{
await Scheduler.Start();
}
public static async Task SchedulerInterval<T>(Dictionary<string, object>? data, int interval,
string group = "defaultGroup") where T : IJob
{
var job = CreateJob<T>(data, group);
var trigger = TriggerBuilder.Create()
.WithIdentity(typeof(T).Name, group)
.StartNow()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(interval).RepeatForever())
.Build();
await Scheduler.ScheduleJob(job, trigger);
}
public static async Task SchedulerCorn<T>(Dictionary<string, object>? data, string? cronExpression,
string group = "defaultGroup") where T : IJob
{
var job = CreateJob<T>(data, group);
var trigger = TriggerBuilder.Create()
.WithIdentity(typeof(T).Name, group)
.StartNow()
.WithSchedule(CronScheduleBuilder.CronSchedule(cronExpression))
.Build();
await Scheduler.ScheduleJob(job, trigger);
}
private static IJobDetail CreateJob<T>(Dictionary<string, object>? data, string group) where T : IJob
{
if (_jobDetails.ContainsKey(typeof(T).Name)) return _jobDetails[typeof(T).Name];
var job = JobBuilder.Create<T>()
.WithIdentity(typeof(T).Name, group)
.Build();
if (data != null && data.Count > 0)
foreach (var item in data)
job.JobDataMap.Add(item.Key, item.Value);
_jobDetails[typeof(T).Name] = job;
return job;
}
public static async Task PauseJob<T>(string group = "defaultGroup")
{
if (_jobDetails.ContainsKey(typeof(T).Name)) await Scheduler.PauseJob(JobKey.Create(typeof(T).Name, group));
}
public static async Task ResumeJob<T>(string group = "defaultGroup")
{
if (_jobDetails.ContainsKey(typeof(T).Name)) await Scheduler.ResumeJob(JobKey.Create(typeof(T).Name, group));
}
public static async Task Shutdown()
{
if (!Scheduler.IsShutdown) await Scheduler.Shutdown();
}
public static async Task TriggerOnceImmediately<T>(string group = "defaultGroup") where T : IJob
{
if (!_jobDetails.ContainsKey(typeof(T).Name)) return;
await Scheduler.TriggerJob(new JobKey(typeof(T).Name, group));
}
}

View File

@ -0,0 +1,11 @@
namespace MasstransferExporter.LogExporter;
public class LogFileExporter
{
public async Task Export(string logFilePath)
{
}
}

View File

@ -34,4 +34,9 @@
<None Remove="obj\**" /> <None Remove="obj\**" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="DataExporter\" />
<Folder Include="ImageExporter\" />
</ItemGroup>
</Project> </Project>