When developing a system, we usually record the log information to facilitate future maintenance. If an error occurs in the system, it is easy to find.
Mature log components, such as log4net, are used during system development. But what I want to introduce today is not the log component, but in some special circumstances, we do not
What should I do if I can capture errors ???
As the title says, we can capture and record errors in application_error of the global file.
Let's take a look at the following sample code:
Protected void application_error (Object sender, eventargs E)
{
// When an unhandled error occurs, get the error
Exception objerr = server. getlasterror (). getbaseexception ();
If (objerr! = NULL)
{
String error = "error page:" + request. url. tostring () + "<br> ";
If (objerr. message! = NULL)
{
Error + = "error message:" + objerr. Message + "<br> ";
}
If (objerr. stacktrace! = NULL)
{
Error + = "Stack message:" + objerr. stacktrace + "<br> ";
}
If (objerr. innerexception! = NULL)
{
Error + = "innerexception" + objerr. innerexception. Message + "<br> ";
}
String strsystemlog = "globalsystemlog. log ";
String strsystemlogpath = server. htmlencode (strsystemlog );
Fileinfo Fi = new fileinfo (strsystemlogpath );
If (file. exists (strsystemlogpath ))
{
Using (streamwriter Sw = Fi. appendtext ())
{
Sw. writeline ();
Sw. writeline (datetime. Now. tow.timestring () + "\ n" + error + "\ n ");
Sw. Close ();
}
}
Else
{
Using (streamwriter Sw = Fi. createtext ())
{
Sw. writeline ();
Sw. writeline (datetime. Now. tow.timestring () + "\ n" + error + "\ n ");
Sw. Close ();
}
}
Server. clearerror ();
Application ["error"] = error;
// Jump to the system error page
Response. Redirect ("~ /Systemerror. aspx ");
}
}