这么做的:
做个页面的基类,所有页面都从它继承,在基页里做统一的错误处理,重要代码这样的:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(this.PhilePage_Load);
this.Error += new System.EventHandler(this.PhilePage_Error);
}
.................
显示错误信息如下:
protected void PhilePage_Error(object sender, System.EventArgs e)
{
string errMsg;
Exception currentError = Server.GetLastError(); errMsg = "<link rel=\"stylesheet\" href=\"/ThePhile/Styles/ThePhile.CSS\">";
errMsg += "<h1>Page Error</h1><hr/>An unexpected error has occurred on this page. The system " +
"administrators have been notified. Please feel free to contact us with the information " +
"surrounding this error.<br/>"+
"The error occurred in: "+Request.Url.ToString()+"<br/>"+
"Error Message: <font class=\"ErrorMessage\">"+ currentError.Message.ToString() + "</font><hr/>"+
"<b>Stack Trace:</b><br/>"+
currentError.ToString(); if ( !(currentError is AppException) )
{
// It is not one of ours, so we cannot guarantee that it has been logged
// into the event log.
LogEvent( currentError.ToString(), EventLogEntryType.Error );
} Response.Write( errMsg );
Server.ClearError();
}
做个页面的基类,所有页面都从它继承,在基页里做统一的错误处理,重要代码这样的:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(this.PhilePage_Load);
this.Error += new System.EventHandler(this.PhilePage_Error);
}
.................
显示错误信息如下:
protected void PhilePage_Error(object sender, System.EventArgs e)
{
string errMsg;
Exception currentError = Server.GetLastError(); errMsg = "<link rel=\"stylesheet\" href=\"/ThePhile/Styles/ThePhile.CSS\">";
errMsg += "<h1>Page Error</h1><hr/>An unexpected error has occurred on this page. The system " +
"administrators have been notified. Please feel free to contact us with the information " +
"surrounding this error.<br/>"+
"The error occurred in: "+Request.Url.ToString()+"<br/>"+
"Error Message: <font class=\"ErrorMessage\">"+ currentError.Message.ToString() + "</font><hr/>"+
"<b>Stack Trace:</b><br/>"+
currentError.ToString(); if ( !(currentError is AppException) )
{
// It is not one of ours, so we cannot guarantee that it has been logged
// into the event log.
LogEvent( currentError.ToString(), EventLogEntryType.Error );
} Response.Write( errMsg );
Server.ClearError();
}
<customErrors defaultRedirect = "allErrors.aspx" mode="Off" >
<error statusCode="404" redirect="404Errors.aspx"/>
<error statusCode="405" redirect="405Errors.aspx"/>
</customErrors>
http://www.developer.com/net/asp/article.php/961301
http://www.codeproject.com/aspnet/errorhandling.asp
<customErrors mode="RemoteOnly" defaultRedirect="AppErrors.aspx">
<error statusCode="404" redirect="NoSuchPage.aspx"/>
<error statusCode="403" redirect="NoAccessAllowed.aspx"/>
</customErrors>
/// 错误处理函数,用于记录错误日志
/// </summary>
public class Error {
//记录错误日志位置
private const string FILE_NAME = "c:\\udslog.txt";/// <summary>
/// 记录日志至文本文件
/// </summary>
/// <param name="message">记录的内容</param>
public static void Log(string message) {
if(File.Exists(FILE_NAME))
{
StreamWriter sr = File.AppendText(FILE_NAME);
sr.WriteLine ("\n");
sr.WriteLine (DateTime.Now.ToString()+message);
sr.Close();
}
else
{
StreamWriter sr = File.CreateText(FILE_NAME);
sr.Close();
}在具体页面中调用:
try
{
有可能出错的代码...
}catch(Exception ex)
{
Response.Write(ex.ToString());
UDS.Components.Error.Log(ex.ToString());
}
比如
try
{
有可能出错的代码...
}catch(Exception ex)
{
string err=ex.ToString();
Response.Write("error.aspx?Error="err);
}
然后 在你的error.aspx 中用 Request.QueryString["Error"];
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
lblErrors.Text = Server.GetLastError().ToString() End Sub不行
Response.Write(s);
2 我用的是 在web。config中改
<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx" />
3。我想把错误显示在错误页上面
4。用这个 lblErrors.Text = Server.GetLastError().ToString()不行大家看清问题 ,先谢谢各位了
Exception LastError;
String ErrMessage;
LastError = Server.GetLastError();if (LastError != null)
ErrMessage = LastError.Message;
else
ErrMessage = "No Errors";Response.Write("Last Error = " + ErrMessage);
'Put user code to initialize the page here
' lblErrors.Text = Request.QueryString("str")
lblErrors.Text = Server.GetLastError().ToString() Server.ClearError() End Sub为什么不行啊
我都急死了
'Put user code to initialize the page here
' lblErrors.Text = Request.QueryString("str")
Dim LastError As Exception
Dim ErrMessage As String LastError = Server.GetLastError() If Not LastError Is Nothing Then
ErrMessage = LastError.Message
Else
ErrMessage = "No Errors"
End If
lblErrors.Text = ErrMessage Server.ClearError() End Sub
捕获不着 错误信息
1.1.在IIS里面修改404等错误对应的页面指向;
1.2.用httpHandler和/或httpModule检测整个站点,遇到非200返回就根据ResultCode返回对应错误页;
1.3.修改web.config,详细请看楼上别人的回答。2.如果你要修改asp.net“调试信息页”:
我不建议你在你的页面工程内做,因为这样如果你的调试页也出错了就很麻烦。asp.net中向调试页、web service页这些特殊页面都是有特定的页面处理的,如果你要改,就先参考系统的调试页来写自己的调试页,然后在web.config或者machine.config中把asp.net调试页指向你自己的。这些查一下MSDN应该都有的。