一 在Global.asax中自定义错误处理并写入系统日志
protected void Application_Error(Object sender, EventArgs e)
{//定制错误信息
string pageurl=Request.Path;
Exception errorinfo=Server.GetLastError();
string message="Url"+pageurl;
message=message+errorinfo.ToString();
string logname="Mycustomlog";
if(!EventLog.SourceExists(logname)){
EventLog.CreateEventSource(logname,logname);
}
EventLog log=new EventLog();
log.Source=logname;
log.WriteEntry(message,EventLogEntryType.Error);
}
二 web.config定义错误处理
<customErrors mode="RemoteOnly" defaulttredirect="err.aspx" >
<error statuscode="500" redirect="500.aspx"></error>
<error statuscode="404" redirect="404.aspx"></error>
</customErrors>
本文介绍如何在ASP.NET应用中实现自定义错误处理,并将错误信息写入系统日志。通过Global.asax文件中的Application_Error事件,可以捕获并格式化错误信息,并使用EventLog组件将其记录下来。此外,还介绍了如何在web.config中配置不同状态码的错误页面。
918

被折叠的 条评论
为什么被折叠?



