ASPNET WebAPI后端框架全局异常跟踪及处理机制,保存系统异常消息日志
ASPNET WebAPI后端框架全局异常跟踪及处理机制,保存系统异常消息日志
用户自定义过滤器注册:
扫一扫加微信:
Web API的简单流程就是从请求到执行到Action并最终作出响应,但是在这个过程有一把【筛子】,那就是过滤器Filter,在从请求到Action这整个流程中使用Filter来进行相应的处理从而作出响应,这对于认证以及授权等是及其重要的,所以说过滤器应用是Web API框架中非常重要的一种实现方式,我们有必要去探讨其原理。
下面代码用于CSFramework.WebAPI框架全局异常跟踪及处理机制,并保存系统异常日志到本地文件。
C# Code:
/// <summary>
/// .NET WebAPI全局异常跟踪,保存系统异常消息日志
/// </summary>
public class WebApiExceptionFilterAttribute : ExceptionFilterAttribute
{
//重写基类的异常处理方法
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
//组合日志文本
string log = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] " +
actionExecutedContext.Exception.GetType().ToString() + ":异常 -> " + actionExecutedContext.Exception.Message;
string stackTrace = "堆栈信息:" + actionExecutedContext.Exception.StackTrace;
//用户返回给前端的错误消息
string error= actionExecutedContext.Exception.GetType().ToString() + ":异常 -> " + actionExecutedContext.Exception.Message;
//写入本地日志
Log2Local.Instance.WriteException(log + " __ " + stackTrace);
if (actionExecutedContext.Exception is HttpResponseException)
{
HttpStatusCode statusCode = (actionExecutedContext.Exception as HttpResponseException).Response.StatusCode;
var oResponse = new HttpResponseMessage();
oResponse.Content = new StringContent("Status:" + ((int)statusCode).ToString() + ":" + statusCode.ToString() + "," + actionExecutedContext.Exception.Message);
oResponse.ReasonPhrase = actionExecutedContext.Exception.Message;
actionExecutedContext.Response = oResponse;
}
else
{
actionExecutedContext.Response = new HttpResponseMessage
{
Content = new StringContent(error),
StatusCode = HttpStatusCode.InternalServerError
};
}
base.OnException(actionExecutedContext);
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// .NET WebAPI全局异常跟踪,保存系统异常消息日志
/// </summary>
public class WebApiExceptionFilterAttribute : ExceptionFilterAttribute
{
//重写基类的异常处理方法
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
//组合日志文本
string log = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] " +
actionExecutedContext.Exception.GetType().ToString() + ":异常 -> " + actionExecutedContext.Exception.Message;
string stackTrace = "堆栈信息:" + actionExecutedContext.Exception.StackTrace;
//用户返回给前端的错误消息
string error= actionExecutedContext.Exception.GetType().ToString() + ":异常 -> " + actionExecutedContext.Exception.Message;
//写入本地日志
Log2Local.Instance.WriteException(log + " __ " + stackTrace);
if (actionExecutedContext.Exception is HttpResponseException)
{
HttpStatusCode statusCode = (actionExecutedContext.Exception as HttpResponseException).Response.StatusCode;
var oResponse = new HttpResponseMessage();
oResponse.Content = new StringContent("Status:" + ((int)statusCode).ToString() + ":" + statusCode.ToString() + "," + actionExecutedContext.Exception.Message);
oResponse.ReasonPhrase = actionExecutedContext.Exception.Message;
actionExecutedContext.Response = oResponse;
}
else
{
actionExecutedContext.Response = new HttpResponseMessage
{
Content = new StringContent(error),
StatusCode = HttpStatusCode.InternalServerError
};
}
base.OnException(actionExecutedContext);
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
用户自定义过滤器注册:
C# Code:
/// <summary>
/// WebApi过滤器统一注册
/// </summary>
public class WebApiFiltersConfig
{
public static void Set(HttpConfiguration config)
{
//捕获系统全局异常
config.Filters.Add(new WebApiExceptionFilterAttribute());
//注册其他过滤器
//...........................
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// <summary>
/// WebApi过滤器统一注册
/// </summary>
public class WebApiFiltersConfig
{
public static void Set(HttpConfiguration config)
{
//捕获系统全局异常
config.Filters.Add(new WebApiExceptionFilterAttribute());
//注册其他过滤器
//...........................
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
.NET WebApi开发框架|MVC框架|后端框架|服务端框架-标准版V1.0
适用开发:快速构建支持多种客户端的服务端程序,支持APP、B/S、C/S跨平台移动终端等。
运行平台:Windows + .NET Framework 4.5
开发工具:Visual Studio 2015+,C#语言
数据库:Microsoft SQLServer 2008R2+(支持多数据库:Oracle/MySql)
运行平台:Windows + .NET Framework 4.5
开发工具:Visual Studio 2015+,C#语言
数据库:Microsoft SQLServer 2008R2+(支持多数据库:Oracle/MySql)
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网