經常使用的過濾器有三種:OnAuthorization、ActionFilterAttribute、ExceptionFilterAttribute異步
本文件主要記錄使用ExceptionFilterAttribute記錄接口的異常日誌,包括請求參數、響應結果,耗時等async
直接上代碼:ide
1 using Newtonsoft.Json; 2 using ServiceRef; 3 using System; 4 using System.Collections.Generic; 5 using System.IO; 6 using System.Linq; 7 using System.Net; 8 using System.Net.Http; 9 using System.Net.Http.Headers; 10 using System.Text; 11 using System.Threading; 12 using System.Threading.Tasks; 13 using System.Web.Http; 14 using System.Web.Http.ExceptionHandling; 15 using System.Web.Http.Filters; 16 17 namespace WebApi.App_Start 18 { 19 /// <summary> 20 /// 異常過濾器 21 /// </summary> 22 public class ExceptionFilter : ExceptionFilterAttribute 23 { 24 25 ///// <summary> 26 ///// 異步寫異常 27 ///// </summary> 28 ///// <param name="actionExecutedContext"></param> 29 ///// <param name="cancellationToken"></param> 30 //public override async Task OnExceptionAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) 31 //{ 32 // //1.獲取異常信息 33 // string errorMsg = actionExecutedContext.Exception.ToString(); 34 35 // //2.對獲取的異常信息進行處理 36 // using (StreamWriter writer = File.AppendText("d:/err.txt")) 37 // { 38 // await writer.WriteLineAsync(errorMsg); 39 // } 40 //} 41 42 public override void OnException(HttpActionExecutedContext actionExcutedContext) 43 { 44 HttpRequestMessage request = actionExcutedContext.Request; 45 string controllerName = actionExcutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName; 46 string actionName = actionExcutedContext.ActionContext.ActionDescriptor.ActionName; 47 string content = request.Content.ReadAsStringAsync().Result; 48 string headers = GetCollections(request.Headers); 49 string exceptionMessage = actionExcutedContext.Exception.Message; 50 string exceptionTrace = actionExcutedContext.Exception.StackTrace; 51 string arguments = JsonConvert.SerializeObject(actionExcutedContext.ActionContext.ActionArguments); 52 string url = request.RequestUri.AbsoluteUri; 53 StringBuilder sb = new StringBuilder(); 54 sb.AppendFormat("URL:{0}\r\n", url); 55 sb.AppendFormat("Controller:{0}\r\n", controllerName); 56 sb.AppendFormat("Action:{0}\r\n", actionName); 57 sb.AppendFormat("Arguments:{0}\r\n", arguments); 58 sb.AppendFormat("Content:{0}\r\n", content); 59 sb.AppendFormat("Headers:{0}\r\n", headers); 60 sb.AppendFormat("ExceptionMessage:{0}\r\n", exceptionMessage); 61 sb.AppendFormat("ExceptionTrace:{0}\r\n", exceptionTrace); 62 actionExcutedContext.Response = new HttpResponseMessage() { Content = new StringContent(sb.ToString()), StatusCode = System.Net.HttpStatusCode.InternalServerError }; 63 //記錄日誌 64 65 string errMsg = ""; 66 sb.Clear(); 67 sb = null; 68 base.OnException(actionExcutedContext); 69 } 70 /// <summary> 71 /// 獲取Action 參數 72 /// </summary> 73 /// <param name="Collections"></param> 74 /// <returns></returns> 75 public string GetCollections(HttpRequestHeaders Collections) 76 { 77 string Parameters = string.Empty; 78 if (Collections == null || Collections.Count() == 0) 79 { 80 return Parameters; 81 } 82 foreach (var coll in Collections) 83 { 84 if (coll.Value.Count() > 0) 85 { 86 Parameters += string.Format("{0}={1}&", coll.Key, coll.Value.FirstOrDefault().ToString()); 87 } 88 } 89 if (!string.IsNullOrWhiteSpace(Parameters) && Parameters.EndsWith("&")) 90 { 91 Parameters = Parameters.Substring(0, Parameters.Length - 1); 92 } 93 return Parameters; 94 } 95 } 96 }