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

通常我们想到操作日志 可能想到的参数可能有 模块 方法 参数内容 操作人 操作时间 操作 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("{: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中的日志

最新文章

  1. Leetcode 60. Permutation Sequence
  2. 万能Adapter以及ViewHolder性能优化
  3. POJ 1753 Flip game ( 高斯消元枚举自由变量)
  4. javascript平时小例子⑥(简易计算器的制作)
  5. Codeforces Round #228 (Div. 1) A
  6. ios开发者证书 签发者无效
  7. oracle之substr函数
  8. cnn softmax regression bp求导
  9. 超炫HTML5 SVG聊天框拖拽弹性摇摆动画特效
  10. HDU5874
  11. linux一键安装
  12. 机器学习-kNN
  13. Unity3D input.GetAxis
  14. Numpy进阶操作
  15. SolrJ的使用
  16. vue 的动画
  17. Eclipse:构造函数不提示才发现
  18. java this的用法
  19. java用Thread方式创建多线程
  20. openshift上传java web项目

热门文章

  1. MySQL 5.7 GA 新特性
  2. photoshop cc 2018破解补丁(pscc2018注册机) 附使用方法
  3. oracle存储过程批量插入测试数据
  4. sql server 小技巧(4) Sql server 排序时让空值排在最后
  5. Apache 的 ab 压测工具快速使用
  6. Hadoop生态圈-Kafka的新API实现生产者-消费者
  7. MongoDB 聚合嵌入的数组(扁平化数据+管道)
  8. ifconfig不显示网卡eth0
  9. Java 在匿名内部函数中为外部函数变量赋值的解决方案
  10. ODPS_ele—UDF Python API