refactor: 抽象操作日志模块
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
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.Framework.Core.CurrentUsers;
|
||||
using Yi.Framework.Core.Helper;
|
||||
using Yi.Framework.Ddd.Repositories;
|
||||
|
||||
|
||||
namespace Yi.Framework.OperLog
|
||||
{
|
||||
public class GlobalOperLogAttribute : ActionFilterAttribute
|
||||
{
|
||||
private ILogger<GlobalOperLogAttribute> _logger;
|
||||
private IRepository<OperationLogEntity> _repository;
|
||||
private ICurrentUser _currentUser;
|
||||
//注入一个日志服务
|
||||
public GlobalOperLogAttribute(ILogger<GlobalOperLogAttribute> logger, IRepository<OperationLogEntity> repository, ICurrentUser currentUser)
|
||||
{
|
||||
_logger = logger;
|
||||
_repository = repository;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public override async 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.Id = SnowflakeHelper.NextId;
|
||||
logEntity.OperIp = ip;
|
||||
//logEntity.OperLocation = location;
|
||||
logEntity.OperType = operLogAttribute.OperType;
|
||||
logEntity.Title = operLogAttribute.Title;
|
||||
logEntity.RequestMethod = context.HttpContext.Request.Method;
|
||||
logEntity.Method = context.HttpContext.Request.Path.Value;
|
||||
logEntity.OperLocation = location;
|
||||
|
||||
|
||||
logEntity.OperUser = _currentUser.UserName;
|
||||
if (operLogAttribute.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 (operLogAttribute.IsSaveRequestData)
|
||||
{
|
||||
//logEntity.RequestParam = context.HttpContext.GetRequestValue(logEntity.RequestMethod);
|
||||
}
|
||||
|
||||
await _repository.InsertAsync(logEntity);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Ddd.Services.Abstract;
|
||||
|
||||
namespace Yi.Framework.OperLog
|
||||
{
|
||||
/// <summary>
|
||||
/// OperationLog服务抽象
|
||||
/// </summary>
|
||||
public interface IOperationLogService : ICrudAppService<OperationLogGetListOutputDto, long, OperationLogGetListInputVo>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.OperLog
|
||||
{
|
||||
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.Framework.OperLog
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class OperLogAttribute : 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)
|
||||
{
|
||||
Title = title;
|
||||
OperType = operationType;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
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;
|
||||
|
||||
namespace Yi.Framework.OperLog
|
||||
{
|
||||
/// <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,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Ddd.Dtos;
|
||||
|
||||
namespace Yi.Framework.OperLog
|
||||
{
|
||||
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,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Ddd.Dtos;
|
||||
|
||||
namespace Yi.Framework.OperLog
|
||||
{
|
||||
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,20 @@
|
||||
using NET.AutoWebApi.Setting;
|
||||
using Yi.Framework.Ddd.Services;
|
||||
using Yi.Framework.Ddd.Dtos;
|
||||
using Yi.Framework.Core.Attributes;
|
||||
|
||||
namespace Yi.Framework.OperLog
|
||||
{
|
||||
/// <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,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="IPTools.China" Version="1.6.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\framework\Yi.Framework.Core\Yi.Framework.Core.csproj" />
|
||||
<ProjectReference Include="..\..\framework\Yi.Framework.Data\Yi.Framework.Data.csproj" />
|
||||
<ProjectReference Include="..\..\framework\Yi.Framework.Ddd\Yi.Framework.Ddd.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,24 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StartupModules;
|
||||
using Yi.Framework.Core;
|
||||
using Yi.Framework.Core.Attributes;
|
||||
|
||||
namespace Yi.Framework.OperLog
|
||||
{
|
||||
[DependsOn(typeof(YiFrameworkCoreModule))]
|
||||
public class YiFrameworkOperLogModule : IStartupModule
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
||||
{
|
||||
services.AddControllers(options => {
|
||||
options.Filters.Add<GlobalOperLogAttribute>();
|
||||
});
|
||||
services.AddSingleton<GlobalOperLogAttribute>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,9 +20,9 @@ TemplateFactory templateFactory = new();
|
||||
//string modelName = "Dictionary";
|
||||
//string nameSpaces = "Yi.RBAC";
|
||||
//List<string> entityNames = new() { "_", "_" };
|
||||
string modelName = "Logs";
|
||||
string modelName = "Setting";
|
||||
string nameSpaces = "Yi.RBAC";
|
||||
List<string> entityNames = new() { "_" };
|
||||
List<string> entityNames = new() { "File" };
|
||||
|
||||
|
||||
foreach (var entityName in entityNames)
|
||||
|
||||
Reference in New Issue
Block a user