自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

巧用ActionFilterAttribute實現(xiàn)API日志的記錄

開發(fā) 前端
使用Func 委托實現(xiàn)API日志的記錄,這次我們使用另外一種方式,F(xiàn)ilter來記錄輸入輸出日志。

[[381754]]

 本文轉載自微信公眾號「UP技術控」,作者conan5566。轉載本文請聯(lián)系UP技術控公眾號。  

背景

上回提到開發(fā)web api的時候,一般是需要記錄api的輸入輸出信息,方便后續(xù)排查問題;使用的是委托的形式進行記錄日志。

使用Func

這次我們使用另外一種方式,F(xiàn)ilter來記錄輸入輸出日志。

實現(xiàn)方式

1、首先在進入action的時候,定義OnActionExecuting。

  1. public override void OnActionExecuting(ActionExecutingContext context) 
  2.         { 
  3.             base.OnActionExecuting(context); 
  4.  
  5.             // 后續(xù)添加了獲取請求的請求體,如果在實際項目中不需要刪除即可 
  6.             long contentLen = context.HttpContext.Request.ContentLength == null ? 0 : context.HttpContext.Request.ContentLength.Value; 
  7.             if (contentLen > 0) 
  8.             { 
  9.                 // 讀取請求體中所有內(nèi)容 
  10.                 System.IO.Stream stream = context.HttpContext.Request.Body; 
  11.                 if (context.HttpContext.Request.Method == "POST"
  12.                 { 
  13.                     stream.Position = 0; 
  14.                 } 
  15.                 byte[] buffer = new byte[contentLen]; 
  16.                 stream.Read(buffer, 0, buffer.Length); 
  17.                 // 轉化為字符串 
  18.                 RequestBody = System.Text.Encoding.UTF8.GetString(buffer); 
  19.             } 
  20.  
  21.             ActionArguments = Newtonsoft.Json.JsonConvert.SerializeObject(context.ActionArguments); 
  22.  
  23.             Stopwatch = new Stopwatch(); 
  24.             Stopwatch.Start(); 
  25.         } 

2、定義Stopwatch ,計算方法的耗時。

 

 

  1. private string ActionArguments { get; set; } 
  2.  
  3.         /// <summary> 
  4.         /// 請求體中的所有值 
  5.         /// </summary> 
  6.         private string RequestBody { get; set; } 
  7.  
  8.         private Stopwatch Stopwatch { get; set; } 

3、結束的時候,把信息打印出來OnActionExecuted。

  1. public override void OnActionExecuted(ActionExecutedContext context) 
  2.         { 
  3.             base.OnActionExecuted(context); 
  4.             Stopwatch.Stop(); 
  5.  
  6.             string url = context.HttpContext.Request.Host + context.HttpContext.Request.Path + context.HttpContext.Request.QueryString; 
  7.             string method = context.HttpContext.Request.Method; 
  8.             string controller = context.Controller.ToString(); 
  9.             string action = context.ActionDescriptor.DisplayName; 
  10.             string token = ""
  11.             if (context.HttpContext.Request != null && context.HttpContext.Request.Headers != null && context.HttpContext.Request.Headers["Authorization"].Count > 0) 
  12.             { 
  13.                 token = context.HttpContext.Request.Headers["Authorization"]; 
  14.             } 
  15.             string qs = ActionArguments; 
  16.             dynamic result = context?.Result?.GetType()?.Name == "EmptyResult" ? new { Value = "無返回結果" } : context?.Result as dynamic
  17.  
  18.             string res = "在返回結果前發(fā)生了異常"
  19.             try 
  20.             { 
  21.                 if (result != null
  22.                 { 
  23.                     res = Newtonsoft.Json.JsonConvert.SerializeObject(result.Value); 
  24.                 } 
  25.             } 
  26.             catch (System.Exception) 
  27.             { 
  28.                 res = "日志未獲取到結果,返回的數(shù)據(jù)無法序列化"
  29.             } 
  30.  
  31.             NLogger.Info( 
  32.                 $"地址:{url} \n " + 
  33.                   $"controller:{controller} \n " + 
  34.                     $"action:{action} \n " + 
  35.                       $"token:{token} \n " + 
  36.                 $"方式:{method} \n " + 
  37.                 $"請求體:{RequestBody} \n " + 
  38.                 $"參數(shù):{qs}\n " + 
  39.                 $"結果:{res}\n " + 
  40.                 $"耗時:{Stopwatch.Elapsed.TotalMilliseconds} 毫秒(指控制器內(nèi)對應方法執(zhí)行完畢的時間)"); 
  41.         } 

4、控制器調(diào)用LogAttribute。

  1. /// <summary> 
  2.     /// 
  3.     /// </summary> 
  4.     [Produces("application/json")] 
  5.     [LogAttribute] 
  6.     [CustomExceptionFilterAttribute] 
  7.     public class DefaultController : Controller 
  8.     { 
  9.     } 

 

 

完整代碼



  1. using CompanyName.ProjectName.Core; 
  2. using Microsoft.AspNetCore.Mvc.Filters; 
  3. using System.Diagnostics; 
  4.  
  5. namespace CompanyName.ProjectName.HttpApi.Host.Code 
  6.     /// <summary> 
  7.     /// 攔截器 
  8.     /// </summary> 
  9.     public class LogAttribute : ActionFilterAttribute 
  10.     { 
  11.         private string ActionArguments { get; set; } 
  12.  
  13.         /// <summary> 
  14.         /// 請求體中的所有值 
  15.         /// </summary> 
  16.         private string RequestBody { get; set; } 
  17.  
  18.         private Stopwatch Stopwatch { get; set; } 
  19.  
  20.         /// <summary> 
  21.         /// 
  22.         /// </summary> 
  23.         /// <param name="context"></param> 
  24.         public override void OnActionExecuting(ActionExecutingContext context) 
  25.         { 
  26.             base.OnActionExecuting(context); 
  27.  
  28.             // 后續(xù)添加了獲取請求的請求體,如果在實際項目中不需要刪除即可 
  29.             long contentLen = context.HttpContext.Request.ContentLength == null ? 0 : context.HttpContext.Request.ContentLength.Value; 
  30.             if (contentLen > 0) 
  31.             { 
  32.                 // 讀取請求體中所有內(nèi)容 
  33.                 System.IO.Stream stream = context.HttpContext.Request.Body; 
  34.                 if (context.HttpContext.Request.Method == "POST"
  35.                 { 
  36.                     stream.Position = 0; 
  37.                 } 
  38.                 byte[] buffer = new byte[contentLen]; 
  39.                 stream.Read(buffer, 0, buffer.Length); 
  40.                 // 轉化為字符串 
  41.                 RequestBody = System.Text.Encoding.UTF8.GetString(buffer); 
  42.             } 
  43.  
  44.             ActionArguments = Newtonsoft.Json.JsonConvert.SerializeObject(context.ActionArguments); 
  45.  
  46.             Stopwatch = new Stopwatch(); 
  47.             Stopwatch.Start(); 
  48.         } 
  49.  
  50.         /// <summary> 
  51.         /// 
  52.         /// </summary> 
  53.         /// <param name="context"></param> 
  54.         public override void OnActionExecuted(ActionExecutedContext context) 
  55.         { 
  56.             base.OnActionExecuted(context); 
  57.             Stopwatch.Stop(); 
  58.  
  59.             string url = context.HttpContext.Request.Host + context.HttpContext.Request.Path + context.HttpContext.Request.QueryString; 
  60.             string method = context.HttpContext.Request.Method; 
  61.             string controller = context.Controller.ToString(); 
  62.             string action = context.ActionDescriptor.DisplayName; 
  63.             string token = ""
  64.             if (context.HttpContext.Request != null && context.HttpContext.Request.Headers != null && context.HttpContext.Request.Headers["Authorization"].Count > 0) 
  65.             { 
  66.                 token = context.HttpContext.Request.Headers["Authorization"]; 
  67.             } 
  68.             string qs = ActionArguments; 
  69.             dynamic result = context?.Result?.GetType()?.Name == "EmptyResult" ? new { Value = "無返回結果" } : context?.Result as dynamic
  70.  
  71.             string res = "在返回結果前發(fā)生了異常"
  72.             try 
  73.             { 
  74.                 if (result != null
  75.                 { 
  76.                     res = Newtonsoft.Json.JsonConvert.SerializeObject(result.Value); 
  77.                 } 
  78.             } 
  79.             catch (System.Exception) 
  80.             { 
  81.                 res = "日志未獲取到結果,返回的數(shù)據(jù)無法序列化"
  82.             } 
  83.  
  84.             NLogger.Info( 
  85.                 $"地址:{url} \n " + 
  86.                   $"controller:{controller} \n " + 
  87.                     $"action:{action} \n " + 
  88.                       $"token:{token} \n " + 
  89.                 $"方式:{method} \n " + 
  90.                 $"請求體:{RequestBody} \n " + 
  91.                 $"參數(shù):{qs}\n " + 
  92.                 $"結果:{res}\n " + 
  93.                 $"耗時:{Stopwatch.Elapsed.TotalMilliseconds} 毫秒(指控制器內(nèi)對應方法執(zhí)行完畢的時間)"); 
  94.         } 
  95.     } 

 

責任編輯:武曉燕 來源: UP技術控
相關推薦

2021-02-03 05:24:44

API日志

2021-02-01 00:04:13

Dictionary數(shù)據(jù)批量

2024-10-22 08:47:03

2024-12-18 12:10:00

2023-03-10 08:59:30

2021-11-17 09:00:00

Kubernetes集群容器

2010-09-25 16:17:25

SQL語句

2024-08-26 08:27:18

2025-04-01 08:20:00

Logging模塊Python日志記錄

2024-10-06 13:49:30

2022-02-08 17:07:54

Spring BooSpring Aop日志記錄

2011-09-23 10:53:09

2024-12-23 13:31:38

2023-05-26 07:08:05

CSS模糊實現(xiàn)文字

2011-08-04 13:31:50

數(shù)據(jù)庫記錄更改日志觸發(fā)器

2024-11-29 10:00:00

Python日志記錄

2024-09-02 00:27:51

SpringAOP自定義

2024-09-09 15:24:26

Redis開發(fā)

2021-11-03 17:10:37

CSS sticky前端代碼

2010-11-18 13:40:48

mysql分頁查詢
點贊
收藏

51CTO技術棧公眾號