直播中
一,在Web.config中填寫出錯時顯示的頁面,可以根據(jù)不同的statusCode顯示不同的出錯頁面。
<customErrors mode="On" //如果設置為Off則出錯只返回錯誤信息,不會跳到自己的指定頁面defaultRedirect="/error/customerrorpage.aspx">
<error statusCode="404" redirect="/error/404Page.aspx"/>
<error statusCode="403" redirect="/error/403page.aspx"/>
</customErrors>
二,在Global.asax文件中添加應用出錯代碼,寫入系統(tǒng)日志文件
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;
//These are the five options that will display a different icon.
Log.WriteEntry(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.FailureAudit, 5);
}
三,現(xiàn)在你可以進行測試了。
我在Default.aspx.cs中產生一個錯誤,果然跳到默認的錯誤頁面!
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
try
{
int y=0;
int x=1/y;
}
catch (Exception Err)
{
throw new Exception("404");//我想產生不同的錯誤,對應web.config中的statusCode,該如何實現(xiàn)?
//Err.
}