asp.net中当服务器出错时显示指定的错误页面同时把错误信息写入系统日志文件的探讨
一,在Web.config中填写出错时显示的页面,可以根据不同的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文件中添加应用出错代码,写入系统日志文件 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, EventLogEnt
|