Display the specified error page, and write the error message to the system log file

xiaoxiao2021-03-06  68

ASP.NET Displays the specified error page while the server error is displayed simultaneously to write the error message to the system log file

First, the page displayed when filling in the error in Web.config, can display different error pages according to different statuscode.

Second, add an app error code to the global.asax file, write the system log file

Protected Void Application_ERROR (Object Sender, Eventargs E)

{

Exception lasterror = server.getlasterror ();

String errmessage = lasterror.tostring ();

String logname = "mylog";

String message = "URL" Request.path "Error:" ErrMessage;

// Create Event Log if it doesn't exist

IF (! EventLog.SourceExists (logname))

{

EventLog.createEventSource (logname, logname);

}

EventLog log = new eventlog ();

Log.source = logname;

//................

Log.writentry (Message, EventLoGentrytype.information, 1);

Log.WriteEntry (Message, EventLoGENTRYTYPE.ERROR, 2);

Log.WriteEntry (Message, EventLoGentrytype.Warning, 3);

Log.WriteEntry (Message, EventLoGENTRYTYPE.SUCCESSAUDIT, 4);

Log.WriteEntry (Message, EventLoGENTRYTYPE.FAILURAUREAUDIT, 5);

}

Third, now you can test it.

I created an error in default.aspx.cs, and I jump to the default error page!

Private Void Page_Load (Object Sender, System.EventArgs E)

{

// put user code to initialize the page

Try

{

INT Y = 0;

INT x = 1 / y;

}

Catch (Exception Err)

{

Throw New Exception ("404"); // I want to have different errors, how to implement StatusCode in Web.Config?

// ERR.

}

转载请注明原文地址:https://www.9cbs.com/read-120073.html

New Post(0)