ASP. net mvc unified log processing makes your application no longer see small yellow pages !, Asp. netmvc

Source: Internet
Author: User

ASP. net mvc unified log processing makes your application no longer see small yellow pages !, Asp. netmvc
Introduction

Exception Handling is a required function of every system. When a program fails, it does not display an inexplicable page or expose the stack information of your code! In ASP. net mvc, how does one correctly handle exceptions and record them?

The answer is that you can use ASP. net mvc Filter (Filter) to capture global exceptions using AOP.

 

Body

 

 

Let's talk about the source code.

Create a folder in the root directory of ASP. net mvc and name it Filter.

 

 

Create a new Class named CustomExceptionAttribute in the Filter and inherit the FilterAttribute Class and the IExceptionFilter interface.

As follows:

 

 

Below is the code

The Code thought is

 

 

 

 

/// <Summary> /// handle system exceptions in a centralized manner /// rewrite the official default HandleErrorAttribute processing /// </summary> public class CustomExceptionAttribute: FilterAttribute, IExceptionFilter {public void OnException (ExceptionContext filterContext) {// check whether an exception if (filterContext. predictionhandled = true) {// skipped return directly after processing;} else {// recorded log var log = new predictionlogmodel (); log. controllerName = (string) filterContext. routeData. values ["co Ntroller "]; // controller Name log. actionName = (string) filterContext. routeData. values ["action"]; // operation name log. loginUser = "temporary test user"; // user log for abnormal operations. url = filterContext. requestContext. httpContext. request. url. localPath; // url log of the exception. logLevel = LogLevel. error; // exception type log. source = filterContext. exception. source; // the log of the object that causes the exception. errorMsg = filterContext. exception. message; // exception details log. stackTrace = filterContext. excep Tion. stackTrace; // exception stack information log. exceptionLog ();} // check whether bool AjaxReq = filterContext is requested asynchronously. httpContext. request. isAjaxRequest (); // jump to the specified exception page var httpException = new HttpException (null, filterContext. exception); switch (httpException. getHttpCode () {case 400: if (! AjaxReq) {ViewResult result = new ViewResult {ViewName = "Page_400", // error page MasterName = null, // specify the master page ViewData = null, // specify the model TempData = filterContext. controller. tempData}; filterContext. result = result; filterContext. httpContext. response. clear (); filterContext. httpContext. response. statusCode = 400;} else {filterContext. result = new HttpStatusCodeResult (System. net. httpStatusCode. badRequest, fi LterContext. Exception. Message);} break; case 401: if (! AjaxReq) {ViewResult result = new ViewResult {ViewName = "Page_401", // error page MasterName = null, // specify the master page ViewData = null, // specify the model TempData = filterContext. controller. tempData}; filterContext. result = result; filterContext. httpContext. response. clear (); filterContext. httpContext. response. statusCode = 401;} else {filterContext. result = new HttpStatusCodeResult (System. net. httpStatusCode. unauthorized, "You Must login to perform this operation");} break; case 403: if (! AjaxReq) {ViewResult result = new ViewResult {ViewName = "Page_403", // error page MasterName = null, // specify the master page ViewData = null, // specify the model TempData = filterContext. controller. tempData}; filterContext. result = result; filterContext. httpContext. response. clear (); filterContext. httpContext. response. statusCode = 403;} else {filterContext. result = new HttpStatusCodeResult (System. net. httpStatusCode. forbidden, "Yo U do not have permission to access this operation ");} break; case 404: if (! AjaxReq) {ViewResult result = new ViewResult {ViewName = "Page_404", // error page MasterName = null, // specify the master page ViewData = null, // specify the model TempData = filterContext. controller. tempData // temporary data}; filterContext. result = result; filterContext. httpContext. response. clear (); filterContext. httpContext. response. statusCode = 404;} else {filterContext. result = new HttpStatusCodeResult (System. net. httpStatusCode. notFoun D, "The requested resource was not found");} break; case 500: if (! AjaxReq) {ViewResult result = new ViewResult {ViewName = "Page_500", // error page MasterName = null, // specify the master page ViewData = null, // specify the model TempData = filterContext. controller. tempData}; filterContext. result = result; filterContext. httpContext. response. clear (); filterContext. httpContext. response. statusCode = 500;} else {filterContext. result = new HttpStatusCodeResult (System. net. httpStatusCode. internalServer Error, filterContext. Exception. Message);} break; default: if (! AjaxReq) {ViewResult result = new ViewResult {ViewName = "Page_500", // error page MasterName = null, // specify the master page ViewData = null, // specify the model TempData = filterContext. controller. tempData}; filterContext. result = result; filterContext. httpContext. response. clear (); filterContext. httpContext. response. statusCode = 500;} else {filterContext. result = new HttpStatusCodeResult (System. net. httpStatusCode. internalServer Error, filterContext. exception. message);} break;} // The setting exception has been handled; otherwise, it will be overwritten by other exception filters. predictionhandled = true; // when you override a derived class, you can obtain or set a value that specifies whether to disable IIS custom errors. FilterContext. HttpContext. Response. TrySkipIisCustomErrors = true ;}

 

 

Finally, modify the content in the FilterConfig. CS file of the App_Start file under the root directory of the MVC project.

 

 

 

Now, all exceptions in your controller will be caught.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.