You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.1 KiB
C#

using Admin.Core.Common;
using Admin.Core.IService.ISys;
using Admin.Core.Model.Sys;
using Microsoft.AspNetCore.Hosting;
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/// <summary>
/// 这里要注意下,命名空间和程序集是一样的,不然反射不到
/// </summary>
namespace Admin.Core.Tasks
{
public class Job_OperateLog_Quartz : JobBase, IJob
{
private readonly ISysOperLogService _operateLogService;
private readonly IWebHostEnvironment _environment;
public Job_OperateLog_Quartz(ISysOperLogService operateLogService, ISysTasksQzService SysTasksQzService, IWebHostEnvironment environment, ISysJobLogService sysJobLogService)
{
_operateLogService = operateLogService;
_environment = environment;
_SysTasksQzService = SysTasksQzService;
_sysJobLogService = sysJobLogService;
}
public async Task Execute(IJobExecutionContext context)
{
var executeLog = await ExecuteJob(context, async () => await Run(context));
}
public async Task Run(IJobExecutionContext context)
{
// 可以直接获取 JobDetail 的值
var jobKey = context.JobDetail.Key;
var jobId = jobKey.Name;
// 也可以通过数据库配置,获取传递过来的参数
JobDataMap data = context.JobDetail.JobDataMap;
List<LogInfo> excLogs = new List<LogInfo>();
var exclogContent = LogLock.ReadLog(Path.Combine(_environment.ContentRootPath, "Log"), $"GlobalExceptionLogs_{DateTime.Now.ToString("yyyMMdd")}.log", Encoding.UTF8);
if (!string.IsNullOrEmpty(exclogContent))
{
excLogs = exclogContent.Split("--------------------------------")
.Where(d => !string.IsNullOrEmpty(d) && d != "\n" && d != "\r\n")
.Select(d => new LogInfo
{
Datetime = (d.Split("|")[0]).Split(',')[0].ObjToDate(),
Content = d.Split("|")[1]?.Replace("\r\n", "<br>"),
LogColor = "EXC",
Import = 9,
}).ToList();
}
var filterDatetime = DateTime.Now.AddHours(-20);
excLogs = excLogs.Where(d => d.Datetime >= filterDatetime).ToList();
var operateLogs = new List<SysOperLog>() { };
excLogs.ForEach(m =>
{
operateLogs.Add(new SysOperLog()
{
OperTime = m.Datetime,
Msg = m.Content,
OperIP = m.IP,
OperName = string.Empty,
Status = SysConst.ENABLE,
});
});
if (operateLogs.Count > 0)
{
var logsIds = await _operateLogService.AddAsync(operateLogs);
}
}
}
}