Asp.net制定错误页
只要在Web.Config中添加如下代码即可。
<system.web>
<customErrors mode="On" defaultRedirect="~/Error.aspx" <!--全局性的 推荐mode为RemoteOnly:本机查看详细错误信息,其他机器查看定制的错误页面 -->
<error statusCode="404" redirect="~/404.html"/> <!--局部性的,可以添加404,403等已知错误-->
</customErrors>
</system.web>
<customErrors mode="On" defaultRedirect="~/Error.aspx" <!--全局性的 推荐mode为RemoteOnly:本机查看详细错误信息,其他机器查看定制的错误页面 -->
<error statusCode="404" redirect="~/404.html"/> <!--局部性的,可以添加404,403等已知错误-->
</customErrors>
</system.web>
可以在Error.aspx中写代码,把错误信息写到日志文件中
using System;
using System.Web;
using System.IO;
namespace GridView入库单管理
{
public partial class Error : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Exception ex = HttpContext.Current.Server.GetLastError(); //获取错误对象;
string line = "------------------------------------------------------------------------------";
File.AppendAllText(Server.MapPath("~/Log.txt"), ex.Message + ex.StackTrace + "\n" + DateTime.Now + "\n" + line);
}
}
}
using System.Web;
using System.IO;
namespace GridView入库单管理
{
public partial class Error : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Exception ex = HttpContext.Current.Server.GetLastError(); //获取错误对象;
string line = "------------------------------------------------------------------------------";
File.AppendAllText(Server.MapPath("~/Log.txt"), ex.Message + ex.StackTrace + "\n" + DateTime.Now + "\n" + line);
}
}
}
}
生成的错误信息格式:(用404测试的)
文件不存在。 在 System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo, String physicalPath, HttpResponse response)
在 System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context, String overrideVirtualPath)
在 System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state)
在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
2011-05-18 18:19:03
在 System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context, String overrideVirtualPath)
在 System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state)
在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
2011-05-18 18:19:03
------------------------------------------------------------------------------