ASP.NET 记录页面错误及自定义错误页

来源:互联网 发布:人工智能的未来txt 编辑:程序博客网 时间:2024/06/10 00:24
 
protected override void OnError(EventArgs e)
{
// At this point we have information about the error
HttpContext ctx = HttpContext.Current;
Exception exception = ctx.Server.GetLastError();
string errorInfo = "错误URL: " + ctx.Request.Url.ToString() + "/n源: " + exception.Source + "/n消息Message: " + exception.Message + "/n堆栈: " + exception.StackTrace;
//ctx.Response.Write(errorInfo);
// --------------------------------------------------
// To let the page finish running we clear the error
// --------------------------------------------------
//ctx.Server.ClearError();
//base.OnError(e);
string LogName = "MyWebError";
if (!System.Diagnostics.EventLog.SourceExists(LogName))
{
System.Diagnostics.EventLog.CreateEventSource(LogName, "App");
}
System.Diagnostics.EventLog el = new System.Diagnostics.EventLog();
el.Source = LogName;
el.WriteEntry(errorInfo, System.Diagnostics.EventLogEntryType.Error);
}
web.config 上修改友好错误页 <customErrors mode="On" defaultRedirect="MyErrorPage.aspx">
</customErrors>