Fork me on GitHub

.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中的日志

 

 

posted @ 2018-10-18 11:28  龙码精神  阅读(1395)  评论(7)    收藏  举报