在这里记录两种实现方式:
第一种:通过重写Controller中的OnExpection的方法来记录日志
新建立一个BaseController 继承Controller
protected override void OnException(ExceptionContext filterContext)
{
ILog log = LogManager.GetLogger("INFO");
log.Info(filterContext.Exception);
filterContext.ExceptionHandled = true;
// 执行基类中的OnException
base.OnException(filterContext);
}
在你需要记录异常的Controller继承BaseController
注意 引用Log4Net的安装包。网上很多案例
第二种通过Attribute修改元数据的方法实现IExceptionFilter的接口
在FilterConfig中注册ExceptionAttribute的方法
两种方法都可以实现