-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Blog.Core.IServices.BASE; | ||
using Blog.Core.Model.Models; | ||
|
||
namespace Blog.Core.IServices | ||
{ | ||
/// <summary> | ||
/// IOperateLogServices | ||
/// </summary> | ||
public interface IOperateLogServices : IBaseServices<OperateLog> | ||
{ | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Blog.Core.IRepository.Base; | ||
using Blog.Core.IServices; | ||
using Blog.Core.Model.Models; | ||
using Blog.Core.Services.BASE; | ||
|
||
namespace Blog.Core.Services | ||
{ | ||
public partial class OperateLogServices : BaseServices<OperateLog>, IOperateLogServices | ||
{ | ||
IBaseRepository<OperateLog> _dal; | ||
public OperateLogServices(IBaseRepository<OperateLog> dal) | ||
{ | ||
this._dal = dal; | ||
base.BaseDal = dal; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
Blog.Core.Tasks/QuartzNet/Jobs/Job_OperateLog_Quartz.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using Blog.Core.Common.Helper; | ||
using Blog.Core.Common.LogHelper; | ||
using Blog.Core.IServices; | ||
using Blog.Core.Model.Models; | ||
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 Blog.Core.Tasks | ||
{ | ||
public class Job_OperateLog_Quartz : JobBase, IJob | ||
{ | ||
private readonly IOperateLogServices _operateLogServices; | ||
private readonly ITasksQzServices _tasksQzServices; | ||
private readonly IWebHostEnvironment _environment; | ||
|
||
public Job_OperateLog_Quartz(IOperateLogServices operateLogServices, ITasksQzServices tasksQzServices, IWebHostEnvironment environment) | ||
{ | ||
_operateLogServices = operateLogServices; | ||
_tasksQzServices = tasksQzServices; | ||
_environment = environment; | ||
} | ||
public async Task Execute(IJobExecutionContext context) | ||
{ | ||
// 可以直接获取 JobDetail 的值 | ||
var jobKey = context.JobDetail.Key; | ||
var jobId = jobKey.Name; | ||
|
||
var executeLog = await ExecuteJob(context, async () => await Run(context, jobId.ObjToInt())); | ||
|
||
// 也可以通过数据库配置,获取传递过来的参数 | ||
JobDataMap data = context.JobDetail.JobDataMap; | ||
} | ||
public async Task Run(IJobExecutionContext context, int jobid) | ||
{ | ||
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(-1); | ||
excLogs = excLogs.Where(d => d.Datetime >= filterDatetime).ToList(); | ||
|
||
var operateLogs = new List<OperateLog>() { }; | ||
excLogs.ForEach(m => | ||
{ | ||
operateLogs.Add(new OperateLog() | ||
{ | ||
LogTime = m.Datetime, | ||
Description = m.Content, | ||
IPAddress = m.IP, | ||
UserId = 0, | ||
IsDeleted = false, | ||
}); | ||
}); | ||
|
||
|
||
if (operateLogs.Count > 0) | ||
{ | ||
var logsIds = await _operateLogServices.Add(operateLogs); | ||
} | ||
|
||
if (jobid > 0) | ||
{ | ||
var model = await _tasksQzServices.QueryById(jobid); | ||
if (model != null) | ||
{ | ||
var list = await _operateLogServices.Query(d => d.IsDeleted == false); | ||
model.RunTimes += 1; | ||
var separator = "<br>"; | ||
model.Remark = | ||
$"【{DateTime.Now}】执行任务【Id:{context.JobDetail.Key.Name},组别:{context.JobDetail.Key.Group}】【执行成功】:异常数{list.Count}{separator}" | ||
+ string.Join(separator, StringHelper.GetTopDataBySeparator(model.Remark, separator, 9)); | ||
|
||
await _tasksQzServices.Update(model); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters