操作日志特性、操作日志全局过滤器搭建
This commit is contained in:
@@ -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}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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("权限不能为空!");
|
||||
|
||||
Reference in New Issue
Block a user