Job_OperateLog_Quartz.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Quartz;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Ropin.Inspection.Tasks.QuartzNet.Jobs
  8. {
  9. public class Job_OperateLog_Quartz : JobBase, IJob
  10. {
  11. private readonly IOperateLogServices _operateLogServices;
  12. private readonly IWebHostEnvironment _environment;
  13. public Job_OperateLog_Quartz(IOperateLogServices operateLogServices, ITasksQzServices tasksQzServices, IWebHostEnvironment environment)
  14. {
  15. _operateLogServices = operateLogServices;
  16. _environment = environment;
  17. _tasksQzServices = tasksQzServices;
  18. }
  19. public async Task Execute(IJobExecutionContext context)
  20. {
  21. var executeLog = await ExecuteJob(context, async () => await Run(context));
  22. }
  23. public async Task Run(IJobExecutionContext context)
  24. {
  25. // 可以直接获取 JobDetail 的值
  26. var jobKey = context.JobDetail.Key;
  27. var jobId = jobKey.Name;
  28. // 也可以通过数据库配置,获取传递过来的参数
  29. JobDataMap data = context.JobDetail.JobDataMap;
  30. List<LogInfo> excLogs = new List<LogInfo>();
  31. var exclogContent = LogLock.ReadLog(Path.Combine(_environment.ContentRootPath, "Log"), $"GlobalExceptionLogs_{DateTime.Now.ToString("yyyMMdd")}.log", Encoding.UTF8);
  32. if (!string.IsNullOrEmpty(exclogContent))
  33. {
  34. excLogs = exclogContent.Split("--------------------------------")
  35. .Where(d => !string.IsNullOrEmpty(d) && d != "\n" && d != "\r\n")
  36. .Select(d => new LogInfo
  37. {
  38. Datetime = (d.Split("|")[0]).Split(',')[0].ObjToDate(),
  39. Content = d.Split("|")[1]?.Replace("\r\n", "<br>"),
  40. LogColor = "EXC",
  41. Import = 9,
  42. }).ToList();
  43. }
  44. var filterDatetime = DateTime.Now.AddHours(-1);
  45. excLogs = excLogs.Where(d => d.Datetime >= filterDatetime).ToList();
  46. var operateLogs = new List<OperateLog>() { };
  47. excLogs.ForEach(m =>
  48. {
  49. operateLogs.Add(new OperateLog()
  50. {
  51. LogTime = m.Datetime,
  52. Description = m.Content,
  53. IPAddress = m.IP,
  54. UserId = 0,
  55. IsDeleted = false,
  56. });
  57. });
  58. if (operateLogs.Count > 0)
  59. {
  60. var logsIds = await _operateLogServices.Add(operateLogs);
  61. }
  62. }
  63. }
  64. }