答案:config.web
'在\eventlog application 根目录下
<configuration>
<customerrors mode="On" defaultredirect="/eventlog/chunfengpage.aspx">
<error statuscode="404" redirect="/eventlog/404Page.aspx"/>
</customerrors>
</configuration>
好了,我们用几个简单的文件来详细地说明这一过程:config.web、 global.asax、default.aspx页面,以及两个简单的错误页面:404page.aspx和 chunfengpage.aspx。下面是这些页面代码及具体使用:
global.asax
' 使用 Application_OnError 事件捕捉错误
<%@ Import Namespace="System.Diagnostics"%>
<%@ Assembly name="System.Diagnostics"%><script language="VB"runat=server>
Public Sub Application_OnError(Sender as Object, E as EventArgs)
Dim LogName As String ="MyLog"
' 捕捉到错误就写入日志
Dim Message As String ="Url"&Request.Path&"Error: "&me. Lasterror .toString()' 如果事件日志不存在就创建
If (Not EventLog.SourceExists(LogName)) Then
EventLog.CreateEventSource(LogName, LogName)
End if' 启动事件日志
Dim Log as New EventLog
Log.Source = LogName
Log.WriteEntry(Message, EventLogEntryType.Error)End Sub
</script>default.aspx
<html>
<head>
</head>
<body>
<form method="post" action="eventlog.aspx" name="form1" id="number">
<asp:Button id="abutton" type="submit" text="按下产生一个错误" runat="server" />
</form>
</body>
</html>eventlog.aspx
' 提交错误<% @Language="VB" %>
<script language="VB" runat=server>
Sub Page_Load(Sender As Object, E As EventArgs)' 声明变量
dim x as integer
dim y as integer
dim z as integer' 设置 x 和 y 的值
x = 1
y = 0' 执行这个用0作除数的运算以提交错误
z = x/yEnd Sub
</script>
<html>
恭喜,资料已添加到事件日志!
</html>chunfengpage.aspx
<html>
<h1> 精彩春风之自个儿定制的错误页面</h1>
</html>404page.aspx
' 捕获 404(未找到)错误
<html>
<h1>404 错误页面</h1>
</html>
好了,都完成了,看看我们的程序是不是正常工作啦?敲入:
http://localhost/eventlog/default.aspx,会显示一个按钮!点击它,看它是否真地会创建日志,并把相关信息写到事件日志里去?
实际上,只有“精彩春风之自个儿定制的错误页面”会被显示, eventlog.aspx页面发生错误被转移到 chunfengpage.aspx. ,URL类如:
http://localhost/eventlog/chunfengpage.aspx?aspxerrorpath=/eventlog/eventlog.aspx
即说明日志确实被创建并写入了东西。
当然,如果我们定制的错误没被打开,实际发生的错误就会替代事件日志显示到浏览器上去。