Use global. asax to record error logs in ASP. NET

Source: Internet
Author: User

Complete glabal. asaxCode:

<% @ Application language = "C #" %>

<SCRIPT runat = "server">

Void application_start (Object sender, eventargs E)
{
// In ApplicationProgramCode that runs at startup

}

Void application_end (Object sender, eventargs E)
{
// Code that runs when the application is closed

}

void application_error (Object sender, eventargs E)
{
// code that runs when an unprocessed 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 + = "error page:" + request. url. tostring () + "<br> ";
Erroraddr = "exception page:" + request. url. tostring ();

Error + = "exception message:" + objerr. Message + "<br> ";
Errorinfo = "exception information:" + objerr. message;
// Error + = "Error Source:" + objerr. Source + "<br> ";
// Error + = "stack information:" + objerr. stacktrace + "<br> ";
Errorsource = "Error Source:" + objerr. source;
Errortrace = "stack information:" + objerr. stacktrace;
Error + = "------------------------------------ <br> ";
Server. clearerror ();
Application ["error"] = error;
// Exclusive mode, because the file can only be written by one process.
System. Io. streamwriter writer = NULL;
Try
{
Lock (this)
{
// Write logs
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;
// Create a directory 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); // create if the file does not exist. True indicates appending.
Writer. writeline ("user IP:" + request. userhostaddress );
// If (session ["Identity"]! = NULL)
//{
// Writer. writeline ("Logon 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 error from http://hi.baidu.com/yanwei99521 ]");
Writer. writeline ("succeeded ("--------------------------------------------------------------------------------------");
}
}
Finally
{
If (writer! = NULL)
Writer. Close ();

}
Response. Redirect ("~ /Errorpagebypurview. aspx? Errorcode = 004 ");
}

Void session_start (Object sender, eventargs E)
{
// The code that runs when the new session starts

}

Void session_end (Object sender, eventargs E)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the web. config file is set
// The session_end event is triggered only when inproc is used. If the session mode is set to StateServer
// Or sqlserver, the event is not triggered.

}

</SCRIPT>

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.