You only need to add the following code in Web. config.
<System. Web>
<Customerrors mode = "on" defaultredirect = "~ /Error. aspx "<! -- The recommended global mode is remoteonly: The detailed error information is displayed on the local machine, and the custom error page is displayed on other machines. -->
<Error statuscode = "404" Redirect = "~ /404.html "/>
<! -- If it is local, you can add known errors such as 404,403 -->
</Customerrors>
</System. Web>
You can write code in error. aspx to write the error information to the log file.
Using system;
Using system. Web;
Using system. IO;
Namespace gridview warehouse receiving ticket management
{
Public partial class error: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Exception EX = httpcontext. Current. server. getlasterror (); // get the error object;
String line = "------------------------------------------------------------------------------";
File. appendalltext (server. mappath ("~ /Log.txt "), Ex. Message + ex. stacktrace +" \ n "+ datetime. Now +" \ n "+ line );
}
}
}
}
Format of the generated error message: (tested with 404)
The file does not exist. In system. Web. staticfilehandler. getfileinfo (string virtualpathwithpathinfo, string physicalpath, httpresponse response)
In system. Web. staticfilehandler. processrequestinternal (httpcontext context, string overridevirtualpath)
In system. Web. defaulthttphandler. beginprocessrequest (httpcontext context, asynccallback callback, object state)
In system. Web. httpapplication. callhandlerexecutionstep. system. Web. httpapplication. iexecutionstep. Execute ()
In system. Web. httpapplication. executestep (iexecutionstep step, Boolean & completedsynchronously)
18:19:03
------------------------------------------------------------------------------