.NetCore中使用ExceptionLess 添加操作日志

上一篇文章已经扩展了日志,下面我们在结合下处理操作日志

通常我们想到操作日志 可能想到的参数可能有 模块 方法 参数内容 操作人 操作时间 操作 Ip 下面我们就来结合这些信息添加操作日志

如果要在代码中每个操作中添加 是非常繁琐的 代码很大部分重复,有AOP思想的应该都知道面向切面的方式处理日志,日志贯穿整个系统

所以我们会想到过滤器,在之前的文章中我用到了拦截器处理,这里我们使用 Filter 过滤器 结合Attribute属性来处理,这里我们主要依靠 IAsyncActionFilter 或者 IActionFilter 来处理

定义个属性类 LogAttribute

 public class LogAttribute : Attribute, IAsyncActionFilter
    {
   }

实现 IAsyncActionFilter 接口 ,定义好属性需要的构造函数 具体代码如下 结合 ExceptionLess添加操作日志

 public class LogAttribute : Attribute, IAsyncActionFilter
    {
        private string MoudleName { get; set; }
        private string MethodName { get; set; }
        /// <summary>
        /// 构造日志类型
        /// </summary>
        /// <param name="Moudle">模块名称</param>
        /// <param name="Method">方法名称</param>
        public LogAttribute(string Moudle, string Method)
        {
            this.MoudleName = Moudle;
            this.MethodName = Method;
        }
        /// <summary>
        /// 添加操作日志 liyouming
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var actiondescriptor = ((ControllerActionDescriptor)context.ActionDescriptor);
            var identity = ((ApiBase)context.Controller).userIdentity;
            var _eLLog = context.HttpContext.RequestServices.GetService(typeof(IELLog)) as IELLog;
            var model = JsonConvert.SerializeObject(context.ActionArguments);
            _eLLog.AddSource($"[{MoudleName}]{MethodName}{string.Format("{0:yyyy年MM月dd日 HH:mm:ss}", DateTime.Now)}")
                .AddMessage("参数内容:" + model ?? "")
                .AddTag(identity.UserId ?? "")
                .AddTag(identity.UserName ?? "")
                .AddTag(actiondescriptor.ControllerName)
                .AddTag(actiondescriptor.ActionName)
                .AddTag(MoudleName)
                .AddTag(MethodName)
                .AddTag(string.Format("{0:yyyy-MM-dd}", DateTime.Now))
                .AddSubmitInfo();

            await next.Invoke();
        }
    }

接下来添加好接口代码

/// <summary>
        /// Demo 添加数据 调用MediatR DDD 实现领域事件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("createdata")]
        [ProducesResponseType(typeof(OperatorResult),(int) HttpStatusCode.OK)]
        [Permission(PermissionCode.DemoCreate)]
        [Log("测试模块","添加测试方法")]
        public async Task<IActionResult> CreateData([FromBody]SchoolDto model)
        {
            var validationResult = _validator.Validate(model);
            if (!validationResult.IsValid)
            {
                return Ok(new OperatorResult
                {
                    Result = ResultType.Fail,
                    Message = string.Join(";", validationResult.Errors)
                });
            }
            //这里用Mapper做映射 
            var school = _mapper.Map<SchoolDto, School>(model);
            school.Id = Guid.NewGuid();
            school.AddClassesDomain(new Classes { CName="Demo", Id=Guid.NewGuid() }); var command = new DemoCreateCommand { com_school = school };
            var result = await _mediator.Send(command);
            
            return Ok(result);
        }

 

下面我们通过API来测试下

下面看下ExceptionLess中的日志

 

 

转载于:https://www.cnblogs.com/liyouming/p/9809483.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值