操作日志特性、操作日志全局过滤器搭建

This commit is contained in:
陈淳
2022-09-29 17:02:34 +08:00
parent 8eef0e410e
commit be7ea64c5a
6 changed files with 87 additions and 15 deletions

View File

@@ -80,16 +80,5 @@ namespace Yi.Framework.ApiMicroservice.Controllers
return Result.Error();
}
}
//[HttpGet]
//public async Task<IActionResult> ExportFile()
//{
// var userdata = await _userService.GetAllEntitiesTrueAsync();
// var userList = userdata.ToList();
// List<string> header = new() { "用户", "密码", "头像", "昵称", "邮箱", "ip", "年龄", "个人介绍", "地址", "手机", "角色" };
// var filename = Common.Helper.ExcelHelper.CreateExcelFromList(userList, header, _env.ContentRootPath.ToString());
// var MimeType = Common.Helper.MimeHelper.GetMimeMapping(filename);
// return new FileStreamResult(new FileStream(Path.Combine(_env.ContentRootPath+@"/wwwroot/excel", filename), FileMode.Open),MimeType);
//}
}
}

View File

@@ -40,7 +40,6 @@ builder.Host.ConfigureLogging(loggingBuilder =>
loggingBuilder.AddFilter("System", Microsoft.Extensions.Logging.LogLevel.Warning);
loggingBuilder.AddFilter("Microsoft", Microsoft.Extensions.Logging.LogLevel.Warning);
loggingBuilder.AddLog4Net("./Config/Log4net.config");
});
#region
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@@ -71,12 +70,16 @@ builder.Services.AddAutoMapperService();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>+<2B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#endregion
builder.Services.AddControllers(optios => {
//optios.Filters.Add<PermissionAttribute>();
//ע<EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD>
optios.Filters.Add<GlobalLogAttribute>();
}).AddJsonFileService();
#region
//Ȩ<>޹<EFBFBD><DEB9><EFBFBD><EFBFBD><EFBFBD>
#endregion
//Ȩ<><C8A8>
builder.Services.AddSingleton<PermissionAttribute>();
//<2F><>־
builder.Services.AddSingleton<GlobalLogAttribute>();
#region
//Swagger<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#endregion

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Common.Enum
{
public enum OperationEnum
{
Query,
Insert,
Update,
Delete
}
}

View File

@@ -0,0 +1,43 @@
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;
namespace Yi.Framework.WebCore.AttributeExtend
{
public class GlobalLogAttribute : ActionFilterAttribute
{
private ILogger<GlobalLogAttribute> _logger;
//注入一个日志服务
public GlobalLogAttribute(ILogger<GlobalLogAttribute> logger)
{
_logger = logger;
}
public override void OnResultExecuted(ResultExecutedContext context)
{
try
{
//查找标签,获取标签对象
if (context.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) return;
LogAttribute logAttribute = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true)
.FirstOrDefault(a => a.GetType().Equals(typeof(LogAttribute))) as LogAttribute;
if (logAttribute == null) return;
string controller = context.RouteData.Values["Controller"].ToString();
string action = context.RouteData.Values["Action"].ToString();
string ip = "127.0.0.1";
string ipData = "深圳";
//日志服务插入一条操作记录即可
}
catch (Exception ex)
{
_logger.LogError(ex, $"操作日志错误:{ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.IdentityModel.JsonWebTokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Yi.Framework.Common.Const;
using Yi.Framework.Common.Enum;
namespace Yi.Framework.WebCore.AttributeExtend
{
[AttributeUsage(AttributeTargets.Method)]
public class LogAttribute : Attribute
{
private OperationEnum OperationType { get; set; }
public LogAttribute(OperationEnum operationType)
{
this.OperationType = operationType;
}
}
}

View File

@@ -26,8 +26,6 @@ namespace Yi.Framework.WebCore.AttributeExtend
/// <exception cref="Exception"></exception>
public override void OnActionExecuting(ActionExecutingContext context)
{
if (string.IsNullOrEmpty(permission))
{
throw new Exception("权限不能为空!");