.net core(.net 6) IActionFilter日志(IAsyncActionFilter)

注:带有注入的扩展类,在标记时需要用

[TypeFilter(typeof(CustomAsyncActionFilterAttribute))] 或者
[ServiceFilter(typeof(CustomAsyncActionFilterAttribute))](需要在IOC容器中注册)

扩展类CustomActionFilterAttribute

using Microsoft.AspNetCore.Mvc.Filters;

namespace Demo02.Utility.Filters
{
    /// <summary>
    /// ActionFilter
    /// </summary>
    public class CustomActionFilterAttribute : Attribute,IActionFilter 
    {
        private readonly ILogger<CustomActionFilterAttribute> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        public CustomActionFilterAttribute(ILogger<CustomActionFilterAttribute> logger)
        {
            _logger = logger;
        }
        /// <summary>
        /// 在方法执行之后
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            _logger.LogInformation("CustomActionFilterAttribute.OnActionExecuted");
            
        }
        /// <summary>
        /// 在方法执行之前
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var controllerName = context.RouteData.Values["controller"];
            var actionName = context.RouteData.Values["action"];
            var queryString = context.HttpContext.Request.QueryString.ToString();
            _logger.LogInformation($"CustomActionFilterAttribute.OnActionExecuting====controllerName:{controllerName},actionName:{actionName},queryString{queryString}");
        }
        
    }
}

扩展类CustomAsyncActionFilterAttribute

using Microsoft.AspNetCore.Mvc.Filters;

namespace Demo02.Utility.Filters
{
    /// <summary>
    /// ActionFilter
    /// </summary>
    public class CustomAsyncActionFilterAttribute : Attribute,IAsyncActionFilter 
    {
        private readonly ILogger<CustomAsyncActionFilterAttribute> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        public CustomAsyncActionFilterAttribute(ILogger<CustomAsyncActionFilterAttribute> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// OnActionExecutionAsync
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            //在方法执行之前
            var controllerName = context.RouteData.Values["controller"];
            var actionName = context.RouteData.Values["action"];
            var queryString = context.HttpContext.Request.QueryString.ToString();
            _logger.LogInformation($"CustomActionFilterAttribute.OnActionExecutionAsync====controllerName:{controllerName},actionName:{actionName},queryString{queryString}");

            await next.Invoke();

            //在...之后

        }
    }
}

Controller Action测试

        /// <summary>
        /// GET AOP测试 ActionFilter日志
        /// </summary>
        /// <returns></returns>
        [HttpGet()]
        [Route("ActionFilter/CustomActionFilter/{id:int}")]
        [TypeFilter(typeof(CustomActionFilterAttribute))]
        public IActionResult GetLog(int id)
        {
            return new JsonResult(new ApiResut<String>()
            {
                Success = true,
                Data = "AOP ActionFilter 测试",
                Time = DateTime.Now.ToString()

            });
        }


        /// <summary>
        /// GET AOP测试 AsyncActionFilter日志异步
        /// </summary>
        /// <returns></returns>
        [HttpGet()]
        [Route("ActionFilter/CustomAsyncActionFilter/{id:int}")]
        [TypeFilter(typeof(CustomAsyncActionFilterAttribute))]
        //[ServiceFilter(typeof(CustomAsyncActionFilterAttribute))]
        public IActionResult AsyncGetLog(int id)
        {
            return new JsonResult(new ApiResut<String>()
            {
                Success = true,
                Data = "AOP AsyncActionFilter 测试",
                Time = DateTime.Now.ToString()

            });
        }

 

posted @ 2023-01-23 22:49  燕钰达  阅读(476)  评论(0编辑  收藏  举报