Construction of ASP.net mvc4+ef5+easyui+unity2.x injection background Management system (13)

Source: Internet
Author: User
Tags exception handling httpcontext log system log try catch

System log and exception handling ③

In the previous section, we talked about how to catch exceptions and log logs, which we said were not captured or forgotten, including 404 errors, and so on, we handled the exception uniformly.

This is to use Application_Error to catch all the exceptions, global exception handling in order to reduce code, unified exception handling, Application_Error located in Global.asax inside,

protected void Application_Error (object sender, EventArgs e)

When an exception is not processed in the call stack and is not processed by the framework code, we say that the exception is unhandled and it will be captured by ASP.net

It captures all application levels of unhandleexception and httpexception (for example, the pages that are accessed do not exist, etc.)

In short, all of the try/catch processing in the page can be removed, but in order to log, in the BLL layer or try Catch

The treatment for this unhandled error is to display a page that lists the details of the unhandled exception.

We write the error into the corresponding file or the database through the Application_Error event.

<summary>///Global exception handling///</summary>///<param name= "Sender" ></param&gt
        ;
            <param name= "E" ></param> protected void Application_Error (object sender, EventArgs e) {
            string s = HttpContext.Current.Request.Url.ToString ();
            HttpServerUtility Server = HttpContext.Current.Server; if (server. GetLastError ()!= null) {Exception LastError = server.
                GetLastError ();
                An exception record can be logged to the database or text, or other logging components can be used.
                Exceptionhander.writeexception (LastError);
                application["LastError"] = LastError;
                int statusCode = HttpContext.Current.Response.StatusCode;
                String exceptionoperator = "/sysexception/error"; try {if (! String.IsNullOrEmpty (Exceptionoperator)) {exceptionoperator = new system.web. Ui. Control ().
                        ResolveUrl (Exceptionoperator); String url = string. Format (' {0} '? Errorurl={1} ", Exceptionoperator, server.
                        UrlEncode (s)); string script = String.Format ("<script language= ' javascript ' type= ' text/javascript ' >window.top.location= ' {0} ')
                        ;</script> ", url);
                        Response.Write (script);
                    Response.End (); } \ Catch {}}}

Hey, I made a mistake convert.toint16 ("dddd"); The following is the wrong display page

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.