-
-
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
261 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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> | ||
/// IAccessTrendLogServices | ||
/// </summary> | ||
public interface IAccessTrendLogServices : IBaseServices<AccessTrendLog> | ||
{ | ||
|
||
} | ||
} | ||
|
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,57 @@ | ||
using SqlSugar; | ||
using System; | ||
|
||
namespace Blog.Core.Model.Models | ||
{ | ||
/// <summary> | ||
/// 用户访问趋势日志 | ||
/// </summary> | ||
public class AccessTrendLog : RootEntityTkey<int> | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
[SugarColumn(Length = 128, IsNullable = true, ColumnDataType = "nvarchar")] | ||
public string User { get; set; } | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
[SugarColumn(Length = 128, IsNullable = true, ColumnDataType = "nvarchar")] | ||
public string IP { get; set; } | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
[SugarColumn(Length = 128, IsNullable = true, ColumnDataType = "nvarchar")] | ||
public string API { get; set; } | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
[SugarColumn(Length = 128, IsNullable = true, ColumnDataType = "nvarchar")] | ||
public string BeginTime { get; set; } | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
[SugarColumn(Length = 128, IsNullable = true, ColumnDataType = "nvarchar")] | ||
public string OPTime { get; set; } | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
[SugarColumn(Length = 128, IsNullable = true, ColumnDataType = "nvarchar")] | ||
public string RequestMethod { get; set; } | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
[SugarColumn(Length = 256, IsNullable = true, ColumnDataType = "nvarchar")] | ||
public string RequestData { get; set; } | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
[SugarColumn(Length = 256, IsNullable = true, ColumnDataType = "nvarchar")] | ||
public string Agent { get; set; } | ||
|
||
/// <summary> | ||
/// 创建时间 | ||
/// </summary> | ||
public DateTime Createdate { get; set; } = DateTime.Now; | ||
} | ||
} |
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 AccessTrendLogServices : BaseServices<AccessTrendLog>, IAccessTrendLogServices | ||
{ | ||
IBaseRepository<AccessTrendLog> _dal; | ||
public AccessTrendLogServices(IBaseRepository<AccessTrendLog> dal) | ||
{ | ||
this._dal = dal; | ||
base.BaseDal = dal; | ||
} | ||
|
||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
Blog.Core.Tasks/QuartzNet/Jobs/Job_AccessTrendLog_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,118 @@ | ||
using Blog.Core.Common.LogHelper; | ||
using Blog.Core.IServices; | ||
using Blog.Core.Model.Models; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Newtonsoft.Json; | ||
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_AccessTrendLog_Quartz : JobBase, IJob | ||
{ | ||
private readonly IAccessTrendLogServices _accessTrendLogServices; | ||
private readonly IWebHostEnvironment _environment; | ||
|
||
public Job_AccessTrendLog_Quartz(IAccessTrendLogServices accessTrendLogServices, ITasksQzServices tasksQzServices, IWebHostEnvironment environment) | ||
{ | ||
_accessTrendLogServices = accessTrendLogServices; | ||
_environment = environment; | ||
_tasksQzServices = tasksQzServices; | ||
} | ||
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; | ||
|
||
var lastestLogDatetime = (await _accessTrendLogServices.Query(null, d => d.Createdate, false)).FirstOrDefault()?.Createdate; | ||
if (lastestLogDatetime == null) | ||
{ | ||
lastestLogDatetime = Convert.ToDateTime("2021-08-01"); | ||
} | ||
|
||
var accLogs = GetAccessLogs().Where(d => d.BeginTime.ObjToDate() >= lastestLogDatetime).ToList(); | ||
|
||
var accTrendLogs = new List<AccessTrendLog>() { }; | ||
accLogs.ForEach(m => | ||
{ | ||
accTrendLogs.Add(new AccessTrendLog() | ||
{ | ||
User = m.User, | ||
API = m.API, | ||
BeginTime = m.BeginTime, | ||
Createdate = DateTime.Now, | ||
IP = m.IP, | ||
RequestMethod = m.RequestMethod?.Length > 50 ? m.RequestMethod.Substring(0, 50) : m.RequestMethod | ||
}); | ||
}); | ||
|
||
|
||
if (accTrendLogs.Count > 0) | ||
{ | ||
var logsIds = await _accessTrendLogServices.Add(accTrendLogs); | ||
} | ||
} | ||
|
||
private List<UserAccessFromFIles> GetAccessLogs() | ||
{ | ||
List<UserAccessFromFIles> userAccessModels = new(); | ||
var accessLogs = LogLock.ReadLog( | ||
Path.Combine(_environment.ContentRootPath, "Log"), "RecordAccessLogs_", Encoding.UTF8, ReadType.Prefix | ||
).ObjToString().TrimEnd(','); | ||
|
||
try | ||
{ | ||
return JsonConvert.DeserializeObject<List<UserAccessFromFIles>>("[" + accessLogs + "]"); | ||
} | ||
catch (Exception) | ||
{ | ||
var accLogArr = accessLogs.Split("\n"); | ||
foreach (var item in accLogArr) | ||
{ | ||
if (item.ObjToString() != "") | ||
{ | ||
try | ||
{ | ||
var accItem = JsonConvert.DeserializeObject<UserAccessFromFIles>(item.TrimEnd(',')); | ||
userAccessModels.Add(accItem); | ||
} | ||
catch (Exception) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
return userAccessModels; | ||
} | ||
|
||
} | ||
public class UserAccessFromFIles | ||
{ | ||
public string User { get; set; } | ||
public string IP { get; set; } | ||
public string API { get; set; } | ||
public string BeginTime { get; set; } | ||
public string OPTime { get; set; } | ||
public string RequestMethod { get; set; } = ""; | ||
public string Agent { get; set; } | ||
} | ||
|
||
} |