The Application_Error code of Global. asax implements error records/error logs.

Source: Internet
Author: User

Use Application_Error of Global. asax to Implement error records

Error Log
Copy codeThe Code is as follows:
Void Application_Error (object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception ex = Server. GetLastError (). GetBaseException ();
StringBuilder str = new StringBuilder ();
Str. Append ("\ r \ n" + DateTime. Now. ToString ("yyyy. MM. dd HH: mm: ss "));
Str. Append ("\ r \ n. Customer Information :");


String ip = "";
If (Request. ServerVariables. Get ("HTTP_X_FORWARDED_FOR ")! = Null)
{
Ip = Request. ServerVariables. Get ("HTTP_X_FORWARDED_FOR"). ToString (). Trim ();
}
Else
{
Ip = Request. ServerVariables. Get ("Remote_Addr"). ToString (). Trim ();
}
Str. Append ("\ r \ n \ tIp:" + ip );
Str. Append ("\ r \ n \ t Browser:" + Request. Browser. Browser. ToString ());
Str. Append ("\ r \ n \ t Browser version:" + Request. Browser. MajorVersion. ToString ());
Str. Append ("\ r \ n \ t Operating System:" + Request. Browser. Platform. ToString ());
Str. Append ("\ r \ n. error message :");
Str. Append ("\ r \ n \ t page:" + Request. Url. ToString ());
Str. Append ("\ r \ n \ t error Message:" + ex. Message );
Str. Append ("\ r \ n \ t Error Source:" + ex. Source );
Str. Append ("\ r \ n \ t exception method:" + ex. TargetSite );
Str. Append ("\ r \ n \ t stack information:" + ex. StackTrace );
Str. Append ("\ r \ n records --------------------------------------------------------------------------------------------------");
// Create a path
String upLoadPath = Server. MapPath ("~ /Log /");
If (! System. IO. Directory. Exists (upLoadPath ))
{
System. IO. Directory. CreateDirectory (upLoadPath );
}
// File creation write error
System. IO. file. appendAllText (upLoadPath + DateTime. now. toString ("yyyy. MM. dd ") + ". log ", str. toString (), System. text. encoding. UTF8 );
// Clear exceptions in time after handling
Server. ClearError ();
// Jump to the error page
Response. Redirect ("~ /Error.html ");
}

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.