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>