常用的过滤器有三种:OnAuthorization、ActionFilterAttribute、ExceptionFilterAttribute
本文件主要记录使用ExceptionFilterAttribute记录接口的异常日志,包括请求参数、响应结果,耗时等
直接上代码:


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_Start18 {19 /// <summary>20 /// 异常过滤器21 /// </summary>22 public class ExceptionFilter : ExceptionFilterAttribute23 { 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.Internal......原文转载:http://www.shaoqun.com/a/856318.html
跨境电商:https://www.ikjzd.com/
汇通达:https://www.ikjzd.com/w/1758
邮乐购物商城:https://www.ikjzd.com/w/1776
全球速卖通:https://www.ikjzd.com/w/81
常用的过滤器有三种:OnAuthorization、ActionFilterAttribute、ExceptionFilterAttribute本文件主要记录使用ExceptionFilterAttribute记录接口的异常日志,包括请求参数、响应结果,耗时等直接上代码:1usingNewtonsoft.Json;2usingServiceRef;3usingSystem;4usingSystem
急速:https://www.ikjzd.com/w/1861
环球易购服装独立站Zaful网红数高达10万!完满布局海外直播赛道:https://www.ikjzd.com/articles/114267
【网经社连载】中国出口跨境电商五种主流物流模式:https://www.ikjzd.com/articles/114268
新手如何建立亚马逊listing?:https://www.ikjzd.com/articles/114269
亚马逊新手卖家,常见上传产品四大误区!:https://www.ikjzd.com/articles/114270
老师把腿张大点我要进去 老师你夹得我好紧好爽:http://lady.shaoqun.com/a/247511.html
小烂货夹得好紧太爽了 3人不停在她体内进进出出:http://lady.shaoqun.com/a/247137.html
我在健身房被3p了 健身房被教练啪到腿软:http://lady.shaoqun.com/m/a/247896.html
深圳宝安劳务工博物馆开馆了吗:http://www.30bags.com/a/481659.html
忍无可忍!亚马逊又现辱华产品,卖家们快坚决抵制:https://www.ikjzd.com/articles/146469
深圳暑假带孩子看什么(19个儿童亲子类演出汇总):http://www.30bags.com/a/481660.html
关键词不够完美?这里有你想要的答案:https://www.ikjzd.com/articles/146453
没有评论:
发表评论
注意:只有此博客的成员才能发布评论。