It takes only three steps to create the error log, log the error logs, and use them, no need to change

Source: Internet
Author: User

The first step, create a folder on the site, name error (for the following code can only use this)

The second step, the right key on the site Add New Item Global Application class, is Global.asax (cannot change name, can only create once),

The contents of the global file: (Take these full examination of the past on the line, in fact, the change is only application_error inside the code, only the Application_Error inside the handcuffs past also line)

<%@ application Language= "C #"%>


<script runat= "Server" >


void Application_Start (object sender, EventArgs e)
{
Code that runs when the application starts


}


void Application_End (object sender, EventArgs e)
{
Code that runs when the application shuts down


}


void Application_Error (object sender, EventArgs e)
{
Code that runs when an unhandled error occurs
Exception objerr = Server.GetLastError (). GetBaseException ();
String error = String. Empty;
String errortime = String. Empty;
String erroraddr = String. Empty;
String errorinfo = String. Empty;
String Errorsource = String. Empty;
String errortrace = String. Empty;


Error + + "Occurrence time:" + System.DateTime.Now.ToString () + "<br>";
Errortime = "Occurrence time:" + System.DateTime.Now.ToString ();


Error + + "Occurrence of abnormal page:" + Request.Url.ToString () + "<br>";
ERRORADDR = "Occurrence of exception page:" + Request.Url.ToString ();


Error + + "Exception information:" + objerr.message + "<br>";
ErrorInfo = "Exception information:" + objerr.message;
Error + = "Source:" +objerr.source+ "<br>";
Error + + "Stack info:" + objerr.stacktrace + "<br>";
Errorsource = "Error Source:" + objerr.source;
Errortrace = "Stack info:" + objerr.stacktrace;
Error + + "--------------------------------------<br>";
Server.ClearError ();
application["Error"] = error;
Exclusive, because a file can be written only by one process.
System.IO.StreamWriter writer = null;
Try
{
Lock (This)
{
Write to log
String year = DateTime.Now.Year.ToString ();
String month = DateTime.Now.Month.ToString ();
String path = string. Empty;
string filename = DateTime.Now.Day.ToString () + ". txt";
Path = Server.MapPath ("~/error/") + year + "/" + month;
Created if the directory does not exist
if (! System.IO.Directory.Exists (PATH))
{
System.IO.Directory.CreateDirectory (path);
}
System.IO.FileInfo file = new System.IO.FileInfo (path + "/" + filename);
if (!file. Exists)
File. Create ();
File. Open (System.IO.FileMode.Append);
writer = new System.IO.StreamWriter (file. FullName, True);//file does not exist to create, true indicates append
Writer. WriteLine ("User ip:" + request.userhostaddress);
if (session["Identity"]!= null)
// {
Writer. WriteLine ("Login account:" system.web.httpcontext.current.session["Identity"]). Yonghuinfo.accountid);
// }
Writer. WriteLine (Errortime);
Writer. WriteLine (ERRORADDR);
Writer. WriteLine (errorinfo);
Writer. WriteLine (Errorsource);
Writer. WriteLine (Errortrace);
Writer. WriteLine ("" "This is the Error from http://hi.baidu.com/yanwei99521");
Writer. WriteLine ("--------------------------------------------------------------------------------------");
}
}
Finally
{
if (writer!= null)
Writer. Close ();


}
Response.Redirect ("~/htmlpage.htm");
}


void Session_Start (object sender, EventArgs e)
{
Code that runs when a new session starts


}


void Session_End (object sender, EventArgs e)
{
Code that runs at the end of the session.
Note: Only the sessionstate mode in the Web.config file is set to
The Session_End event is raised when InProc. If the session mode is set to StateServer
or SQL Server, the event is not raised.



}

</script>

The third step, add an HTM file below the website, as the error page, named Htmlpage.htm, also for the above code only named this

Test results

As long as the error will jump out of the error page, as follows:


You can then modify the code according to the error log, based on the current lookup error log, in the error folder, which is the following figure:



Oh ~ ~ These three parts are done, just try it.

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.