您现在的位置是:网站首页> 编程资料编程资料
ASP.NET 通过拦截器记录错误日志的示例代码_实用技巧_
2023-05-24
347人已围观
简介 ASP.NET 通过拦截器记录错误日志的示例代码_实用技巧_
前言
主要是记录一下实现的错误日志拦截,可以在拦截器里面控制返回的信息,把错误信息处理后返回给请求端。
拦截器
拦截器又称过滤器。
asp.net mvc本身是自带3种拦截器:Action拦截器、Result拦截器、Exception拦截器。 应用中常见的拦截器有日志拦截器(Action拦截器)和异常处理拦截器(Exception拦截器)。
java里spring mvc也常用拦截器来做些非干预业务逻辑的事,诸如实现HandlerInterceptor接口。
拦截器是AOP(面向切面编程)的一种应用。
拦截器要解决的问题:
1.代码复用。拦截器可被复用
2.职责单一。比如厨师只负责炒菜,不管前期的洗菜、后续的送菜工作。菜变质了也是直接喊一声就有人来处理。
这次我们用来记录错误日志
代码实战
拦截器
////// 接口异常捕捉过滤器 /// [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)] public class ApiErrorHandleAttribute : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext actionExecutedContext) { base.OnException(actionExecutedContext); actionExecutedContext.Response = GetResponse(actionExecutedContext); } ////// 捕捉异常后响应方法 /// private HttpResponseMessage GetResponse(HttpActionExecutedContext actionExecutedContext) { var requesthost = actionExecutedContext.ActionContext.Request.RequestUri.ToString();//当前请求的地址,包括ip加接口地址 var method = actionExecutedContext.ActionContext.Request.Method.ToString();//当前请求是POST/GET/PUT/DELETE …… var controller = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerType.FullName;//当前请求的控制器所在全路径:xxx.WebAPI.Controllers.xxxController var action = actionExecutedContext.ActionContext.ActionDescriptor.ActionName; //当前请求的方法 var paramters = actionExecutedContext.ActionContext.ActionArguments; //获取当前请求的参数 var ip = HttpContext.Current.Request.UserHostAddress; LogHelper.Error($"错误信息:URL:{actionExecutedContext.Request.RequestUri},--参数信息:{paramters.ToJson()},--actionExecutedContext.Exception:{actionExecutedContext.Exception.ToJson()}"); var response = new { code = 506, message = $"{actionExecutedContext.Exception.Message},URL:{actionExecutedContext.Request.RequestUri}", ex = actionExecutedContext.Exception }; return JsonHelper.ToHttpResponseMessage(response); } }
工具类方法
public static class JsonHelper { /// /// 转化为json格式的HttpResponseMessage /// public static HttpResponseMessage ToHttpResponseMessage(object obj) { string str; if (obj is string || obj is char) { str = obj.ToString(); } else { str = obj.ToJson(); } var result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") }; return result; } /// /// 转化为json字符串,默认的时间格式 /// /// 要被转化的对象 /// json字符串 public static string ToJson(this object obj) { return JsonConvert.SerializeObject(obj, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, DateFormatString = "yyyy-MM-dd HH:mm:ss" }); } }以上就是ASP.NET 通过拦截器记录错误日志的示例代码的详细内容,更多关于ASP.NET 记录错误日志的资料请关注其它相关文章!
您可能感兴趣的文章:
相关内容
- ASP.NET Core扩展库之Http通用扩展库的使用详解_实用技巧_
- 时间轻松学会.NET Core操作ElasticSearch7的方法_实用技巧_
- Asp.Net Core添加请求头自定义认证的示例_实用技巧_
- ASP.NET Core实现自动依赖注入_实用技巧_
- ASP.NET Core中使用令牌桶限流的实现_实用技巧_
- 如何在ASP.NET Core中使用ViewComponent_实用技巧_
- ASP.NET 上传文件到共享文件夹的示例_实用技巧_
- ASP.NET Core扩展库之Http日志的使用详解_实用技巧_
- ASP.NET 上传文件导入Excel的示例_实用技巧_
- .Net Core路由处理的知识点与方法总结_实用技巧_
