feat: 添加操作日志搭建
This commit is contained in:
@@ -20,7 +20,7 @@ TemplateFactory templateFactory = new();
|
|||||||
//string modelName = "Dictionary";
|
//string modelName = "Dictionary";
|
||||||
//string nameSpaces = "Yi.RBAC";
|
//string nameSpaces = "Yi.RBAC";
|
||||||
//List<string> entityNames = new() { "_", "_" };
|
//List<string> entityNames = new() { "_", "_" };
|
||||||
string modelName = "Setting";
|
string modelName = "Logs";
|
||||||
string nameSpaces = "Yi.RBAC";
|
string nameSpaces = "Yi.RBAC";
|
||||||
List<string> entityNames = new() { "_" };
|
List<string> entityNames = new() { "_" };
|
||||||
|
|
||||||
|
|||||||
@@ -94,6 +94,11 @@
|
|||||||
登录信息
|
登录信息
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="T:Yi.RBAC.Application.Contracts.Logs.IOperationLogService">
|
||||||
|
<summary>
|
||||||
|
OperationLog服务抽象
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
<member name="T:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigCreateInputVo">
|
<member name="T:Yi.RBAC.Application.Contracts.Setting.Dtos.ConfigCreateInputVo">
|
||||||
<summary>
|
<summary>
|
||||||
Config输入创建对象
|
Config输入创建对象
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.Ddd.Dtos;
|
||||||
|
using Yi.RBAC.Domain.Shared.Logs;
|
||||||
|
|
||||||
|
namespace Yi.RBAC.Application.Contracts.Logs.Dtos
|
||||||
|
{
|
||||||
|
public class OperationLogGetListInputVo : PagedAllResultRequestDto
|
||||||
|
{
|
||||||
|
public string? Title { get; set; }
|
||||||
|
public OperEnum OperType { get; set; }
|
||||||
|
public string? RequestMethod { get; set; }
|
||||||
|
public string? OperUser { get; set; }
|
||||||
|
public string? OperIp { get; set; }
|
||||||
|
public string? OperLocation { get; set; }
|
||||||
|
public string? Method { get; set; }
|
||||||
|
public string? RequestParam { get; set; }
|
||||||
|
public string? RequestResult { get; set; }
|
||||||
|
public DateTime CreationTime { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.Ddd.Dtos;
|
||||||
|
using Yi.RBAC.Domain.Shared.Logs;
|
||||||
|
|
||||||
|
namespace Yi.RBAC.Application.Contracts.Logs.Dtos
|
||||||
|
{
|
||||||
|
public class OperationLogGetListOutputDto : IEntityDto<long>
|
||||||
|
{
|
||||||
|
public long Id { get; set; }
|
||||||
|
public string? Title { get; set; }
|
||||||
|
public OperEnum OperType { get; set; }
|
||||||
|
public string? RequestMethod { get; set; }
|
||||||
|
public string? OperUser { get; set; }
|
||||||
|
public string? OperIp { get; set; }
|
||||||
|
public string? OperLocation { get; set; }
|
||||||
|
public string? Method { get; set; }
|
||||||
|
public string? RequestParam { get; set; }
|
||||||
|
public string? RequestResult { get; set; }
|
||||||
|
public DateTime CreationTime { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Yi.RBAC.Application.Contracts.Logs.Dtos;
|
||||||
|
using Yi.Framework.Ddd.Services.Abstract;
|
||||||
|
|
||||||
|
namespace Yi.RBAC.Application.Contracts.Logs
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// OperationLog服务抽象
|
||||||
|
/// </summary>
|
||||||
|
public interface IOperationLogService : ICrudAppService<OperationLogGetListOutputDto, long, OperationLogGetListInputVo >
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -179,6 +179,11 @@
|
|||||||
<param name="state"></param>
|
<param name="state"></param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="T:Yi.RBAC.Application.Logs.OperationLogService">
|
||||||
|
<summary>
|
||||||
|
OperationLog服务实现
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
<member name="T:Yi.RBAC.Application.Setting.ConfigService">
|
<member name="T:Yi.RBAC.Application.Setting.ConfigService">
|
||||||
<summary>
|
<summary>
|
||||||
Config服务实现
|
Config服务实现
|
||||||
|
|||||||
@@ -28,5 +28,6 @@ namespace Yi.RBAC.Application.Logs
|
|||||||
.ToPageListAsync(input.PageNum, input.PageSize, total);
|
.ToPageListAsync(input.PageNum, input.PageSize, total);
|
||||||
return new PagedResultDto<LoginLogGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
|
return new PagedResultDto<LoginLogGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
using Yi.RBAC.Application.Contracts.Logs;
|
||||||
|
using NET.AutoWebApi.Setting;
|
||||||
|
using Yi.RBAC.Application.Contracts.Logs.Dtos;
|
||||||
|
using Yi.RBAC.Domain.Logs.Entities;
|
||||||
|
using Yi.Framework.Ddd.Services;
|
||||||
|
using Yi.Framework.Model.RABC.Entitys;
|
||||||
|
using Yi.Framework.Ddd.Dtos;
|
||||||
|
|
||||||
|
namespace Yi.RBAC.Application.Logs
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// OperationLog服务实现
|
||||||
|
/// </summary>
|
||||||
|
[AppService]
|
||||||
|
public class OperationLogService : CrudAppService<OperationLogEntity, OperationLogGetListOutputDto, long, OperationLogGetListInputVo >,
|
||||||
|
IOperationLogService, IAutoApiService
|
||||||
|
{
|
||||||
|
public override Task<PagedResultDto<OperationLogGetListOutputDto>> GetListAsync(OperationLogGetListInputVo input)
|
||||||
|
{
|
||||||
|
return base.GetListAsync(input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Yi.RBAC.Domain.Shared.Logs
|
||||||
|
{
|
||||||
|
public enum OperEnum
|
||||||
|
{
|
||||||
|
Insert = 1,
|
||||||
|
Update = 2,
|
||||||
|
Delete = 3,
|
||||||
|
Auth = 4,
|
||||||
|
Export = 5,
|
||||||
|
Import = 6,
|
||||||
|
ForcedOut = 7,
|
||||||
|
GenerateCode = 8,
|
||||||
|
ClearData = 9
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Yi.RBAC.Domain.Shared.Logs
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
|
public class OperLogAttribute : System.Attribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 操作类型
|
||||||
|
/// </summary>
|
||||||
|
public OperEnum OperType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 日志标题(模块)
|
||||||
|
/// </summary>
|
||||||
|
public string Title { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否保存请求数据
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSaveRequestData { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否保存返回数据
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSaveResponseData { get; set; } = true;
|
||||||
|
|
||||||
|
public OperLogAttribute(string title, OperEnum operationType)
|
||||||
|
{
|
||||||
|
this.Title = title;
|
||||||
|
this.OperType = operationType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -790,5 +790,55 @@
|
|||||||
描述
|
描述
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="T:Yi.Framework.Model.RABC.Entitys.OperationLogEntity">
|
||||||
|
<summary>
|
||||||
|
操作日志表
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.Title">
|
||||||
|
<summary>
|
||||||
|
操作模块
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.OperType">
|
||||||
|
<summary>
|
||||||
|
操作类型
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.RequestMethod">
|
||||||
|
<summary>
|
||||||
|
请求方法
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.OperUser">
|
||||||
|
<summary>
|
||||||
|
操作人员
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.OperIp">
|
||||||
|
<summary>
|
||||||
|
操作Ip
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.OperLocation">
|
||||||
|
<summary>
|
||||||
|
操作地点
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.Method">
|
||||||
|
<summary>
|
||||||
|
操作方法
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.RequestParam">
|
||||||
|
<summary>
|
||||||
|
请求参数
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="P:Yi.Framework.Model.RABC.Entitys.OperationLogEntity.RequestResult">
|
||||||
|
<summary>
|
||||||
|
请求结果
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
</members>
|
</members>
|
||||||
</doc>
|
</doc>
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using SqlSugar;
|
||||||
|
using Yi.Framework.Data.Auditing;
|
||||||
|
using Yi.Framework.Ddd.Entities;
|
||||||
|
using Yi.RBAC.Domain.Shared.Logs;
|
||||||
|
|
||||||
|
namespace Yi.Framework.Model.RABC.Entitys
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 操作日志表
|
||||||
|
///</summary>
|
||||||
|
[SugarTable("OperationLog")]
|
||||||
|
public class OperationLogEntity : IEntity<long>, ICreationAuditedObject
|
||||||
|
{
|
||||||
|
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]
|
||||||
|
public long Id { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 操作模块
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "Title")]
|
||||||
|
public string? Title { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 操作类型
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "OperType")]
|
||||||
|
public OperEnum OperType { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 请求方法
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "RequestMethod")]
|
||||||
|
public string? RequestMethod { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 操作人员
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "OperUser")]
|
||||||
|
public string? OperUser { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 操作Ip
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "OperIp")]
|
||||||
|
public string? OperIp { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 操作地点
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "OperLocation")]
|
||||||
|
public string? OperLocation { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 操作方法
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "Method")]
|
||||||
|
public string? Method { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 请求参数
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "RequestParam")]
|
||||||
|
public string? RequestParam { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 请求结果
|
||||||
|
///</summary>
|
||||||
|
[SugarColumn(ColumnName = "RequestResult")]
|
||||||
|
public string? RequestResult { get; set; }
|
||||||
|
|
||||||
|
public DateTime CreationTime { get; set; }
|
||||||
|
|
||||||
|
public long? CreatorId { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
using IPTools.Core;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.AspNetCore.Extensions;
|
||||||
|
using Yi.RBAC.Domain.Shared.Logs;
|
||||||
|
|
||||||
|
namespace Yi.RBAC.Domain.Logs
|
||||||
|
{
|
||||||
|
public class GlobalOperLogAttribute : ActionFilterAttribute
|
||||||
|
{
|
||||||
|
private ILogger<GlobalOperLogAttribute> _logger;
|
||||||
|
//注入一个日志服务
|
||||||
|
public GlobalOperLogAttribute(ILogger<GlobalOperLogAttribute> logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnResultExecuted(ResultExecutedContext context)
|
||||||
|
{
|
||||||
|
//判断标签是在方法上
|
||||||
|
if (context.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) return;
|
||||||
|
|
||||||
|
//查找标签,获取标签对象
|
||||||
|
OperLogAttribute? operLogAttribute = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true)
|
||||||
|
.FirstOrDefault(a => a.GetType().Equals(typeof(OperLogAttribute))) as OperLogAttribute;
|
||||||
|
//空对象直接返回
|
||||||
|
if (operLogAttribute is null) return;
|
||||||
|
|
||||||
|
////获取控制器名
|
||||||
|
//string controller = context.RouteData.Values["Controller"].ToString();
|
||||||
|
|
||||||
|
////获取方法名
|
||||||
|
//string action = context.RouteData.Values["Action"].ToString();
|
||||||
|
|
||||||
|
//获取Ip
|
||||||
|
string ip = context.HttpContext.GetClientIp();
|
||||||
|
|
||||||
|
//根据ip获取地址
|
||||||
|
|
||||||
|
var ipTool = IpTool.Search(ip);
|
||||||
|
string location = ipTool.Province + " " + ipTool.City;
|
||||||
|
|
||||||
|
//日志服务插入一条操作记录即可
|
||||||
|
|
||||||
|
//var logEntity = new OperationLogEntity();
|
||||||
|
|
||||||
|
//logEntity.OperIp = ip;
|
||||||
|
////logEntity.OperLocation = location;
|
||||||
|
//logEntity.OperType = logAttribute.OperType.GetHashCode();
|
||||||
|
//logEntity.Title = logAttribute.Title;
|
||||||
|
//logEntity.RequestMethod = context.HttpContext.Request.Method;
|
||||||
|
//logEntity.Method = context.HttpContext.Request.Path.Value;
|
||||||
|
//logEntity.IsDeleted = false;
|
||||||
|
//logEntity.OperUser= context.HttpContext.GetUserNameInfo();
|
||||||
|
//logEntity.OperLocation = location;
|
||||||
|
//if (logAttribute.IsSaveResponseData)
|
||||||
|
//{
|
||||||
|
// if (context.Result is ContentResult result && result.ContentType == "application/json")
|
||||||
|
// {
|
||||||
|
// logEntity.RequestResult = result.Content?.Replace("\r\n", "").Trim();
|
||||||
|
// }
|
||||||
|
// if (context.Result is JsonResult result2)
|
||||||
|
// {
|
||||||
|
// logEntity.RequestResult = result2.Value?.ToString();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (context.Result is ObjectResult result3)
|
||||||
|
// {
|
||||||
|
// logEntity.RequestResult = JsonHelper.ObjToStr(result3.Value);
|
||||||
|
// }
|
||||||
|
|
||||||
|
//}
|
||||||
|
|
||||||
|
//if (logAttribute.IsSaveRequestData)
|
||||||
|
//{
|
||||||
|
// logEntity.RequestParam = context.HttpContext.GetRequestValue(logEntity.RequestMethod);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//_operationLogService._repository.InsertReturnSnowflakeId(logEntity);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ using Yi.Framework.Core.Attributes;
|
|||||||
using Yi.Framework.Data;
|
using Yi.Framework.Data;
|
||||||
using Yi.Framework.EventBus;
|
using Yi.Framework.EventBus;
|
||||||
using Yi.Framework.ThumbnailSharp;
|
using Yi.Framework.ThumbnailSharp;
|
||||||
|
using Yi.RBAC.Domain.Logs;
|
||||||
using Yi.RBAC.Domain.Shared;
|
using Yi.RBAC.Domain.Shared;
|
||||||
|
|
||||||
namespace Yi.RBAC.Domain
|
namespace Yi.RBAC.Domain
|
||||||
@@ -30,6 +31,10 @@ namespace Yi.RBAC.Domain
|
|||||||
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
||||||
{
|
{
|
||||||
services.AddHeiCaptcha();
|
services.AddHeiCaptcha();
|
||||||
|
services.AddControllers(options => {
|
||||||
|
options.Filters.Add<GlobalOperLogAttribute>();
|
||||||
|
});
|
||||||
|
services.AddSingleton<GlobalOperLogAttribute>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user