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 ");
}