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>
;
<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