</param> public override void OnActionExecuting(ActionExecutingCon..." /> .net Core 3.1 Log拦截器JackerKun
asp.net,  dotnet,  

.net Core 3.1 Log拦截器

/// <summary>
  /// 用于拦截入参的特性
  /// </summary>
  public class LogAttribute : ActionFilterAttribute
  {
 
      /// <summary>
      /// 拦截入参
      /// </summary>
      /// <param name="context"></param>
      public override void OnActionExecuting(ActionExecutingContext context)
      {
          try
          {
              string _controlName = ((ControllerActionDescriptor)context.ActionDescriptor).ControllerName;
              string _actionName = ((ControllerActionDescriptor)context.ActionDescriptor).ActionName;
              object _arguments = ((ControllerActionDescriptor)context.ActionDescriptor);
              JCLog.JCLog _log = new JCLog.JCLog(_controlName);
              _log.WriteLog(string.Format("On Start:   controller:{0}  action:{1}    args:{2}", _controlName, _actionName, JsonConvert.SerializeObject(context.ActionArguments)));
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
      }
 
      /// <summary>
      /// 拦截 出参
      /// </summary>
      /// <param name="context"></param>
      public override void OnResultExecuted(ResultExecutedContext context)
      {
          try
          {
              string _controlName = ((ControllerActionDescriptor)context.ActionDescriptor).ControllerName;
              string _actionName = ((ControllerActionDescriptor)context.ActionDescriptor).ActionName;
              var _arguments = ((ControllerActionDescriptor)context.ActionDescriptor);
              var result = context.Result;
              JCLog.JCLog _log = new JCLog.JCLog(_controlName);
              _log.WriteLog(string.Format("On End:   controller:{0}  action:{1}    args:{2}", _controlName, _actionName, JsonConvert.SerializeObject(result)));
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
      }
  }

调用方法:

在控制器前面 加上“ [Log]” 属性即可



留言

您的电子邮箱地址不会被公开。 必填项已用*标注