From fa4e0b3752eb6ea520a6119a21b34647e35e5418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= Date: Tue, 21 Feb 2023 19:55:05 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Setting/ConfigService.cs | 2 + .../Logs/GlobalOperLogAttribute.cs | 72 ++++++++++--------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Setting/ConfigService.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Setting/ConfigService.cs index 5a3f3a29..3cf4e1f8 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Setting/ConfigService.cs +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Application/Setting/ConfigService.cs @@ -7,6 +7,7 @@ using Yi.Framework.Ddd.Dtos; using SqlSugar; using Yi.RBAC.Application.Contracts.Identity.Dtos; using Yi.RBAC.Domain.Identity.Entities; +using Microsoft.AspNetCore.Mvc; namespace Yi.RBAC.Application.Setting { @@ -14,6 +15,7 @@ namespace Yi.RBAC.Application.Setting /// Config服务实现 /// [AppService] + public class ConfigService : CrudAppService, IConfigService, IAutoApiService { diff --git a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/GlobalOperLogAttribute.cs b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/GlobalOperLogAttribute.cs index 25b5c6c1..d7cc28a0 100644 --- a/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/GlobalOperLogAttribute.cs +++ b/Yi.Framework.Net6/src/project/rbac/Yi.RBAC.Domain/Logs/GlobalOperLogAttribute.cs @@ -9,6 +9,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Yi.Framework.AspNetCore.Extensions; +using Yi.Framework.Core.CurrentUsers; +using Yi.Framework.Ddd.Repositories; +using Yi.Framework.Model.RABC.Entitys; using Yi.RBAC.Domain.Shared.Logs; namespace Yi.RBAC.Domain.Logs @@ -16,13 +19,17 @@ namespace Yi.RBAC.Domain.Logs public class GlobalOperLogAttribute : ActionFilterAttribute { private ILogger _logger; + private IRepository _repository; + private ICurrentUser _currentUser; //注入一个日志服务 - public GlobalOperLogAttribute(ILogger logger) + public GlobalOperLogAttribute(ILogger logger, IRepository repository,ICurrentUser currentUser) { _logger = logger; + _repository = repository; + _currentUser=currentUser; } - public override void OnResultExecuted(ResultExecutedContext context) + public override async void OnResultExecuted(ResultExecutedContext context) { //判断标签是在方法上 if (context.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) return; @@ -49,41 +56,42 @@ namespace Yi.RBAC.Domain.Logs //日志服务插入一条操作记录即可 - //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(); + var logEntity = new OperationLogEntity(); + logEntity.Id = SnowflakeHelper.NextId; + logEntity.OperIp = ip; //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(); - // } + logEntity.OperType = operLogAttribute.OperType; + logEntity.Title = operLogAttribute.Title; + logEntity.RequestMethod = context.HttpContext.Request.Method; + logEntity.Method = context.HttpContext.Request.Path.Value; + logEntity.OperLocation = location; - // if (context.Result is ObjectResult result3) - // { - // logEntity.RequestResult = JsonHelper.ObjToStr(result3.Value); - // } - //} + 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 (logAttribute.IsSaveRequestData) - //{ - // logEntity.RequestParam = context.HttpContext.GetRequestValue(logEntity.RequestMethod); - //} + if (context.Result is ObjectResult result3) + { + logEntity.RequestResult = JsonHelper.ObjToStr(result3.Value); + } - //_operationLogService._repository.InsertReturnSnowflakeId(logEntity); + } + + if (operLogAttribute.IsSaveRequestData) + { + //logEntity.RequestParam = context.HttpContext.GetRequestValue(logEntity.RequestMethod); + } + + await _repository.InsertAsync(logEntity); }