asp.net同以前的asp一样,当服务器上发生了一个运行时间或编译时间错误时,就会生成一个html 错误页面。但是与asp不同,asp.net格外关注的是:要确保在默认状态下,不会因为这个错误的发生而泄露“安全”信息。尤其是如果你从一个远程机器上点击服务器的话。“out of the box”型的错误处理设置将不会导致显示远程机器的编译器信息、泄露配置信息、文件名、堆栈记录 、源代码或线性数据。相反,远程用户只会看到一个如“发生了应用程序错误”的普通信息。要想查看错误细节,用户必须要:1)从本地服务器再次点击页面,或者是 2)在机器或应用程序的config.web文件中修改配置的设置来允许远程访问:
<%@ import namespace="system.diagnostics" %> <script language="vb" runat=server> sub application_error(sender as object, e as eventargs) 'obtain the url of the request dim pageurl as string = request.path 'obtain the exception object for the error dim errorinfo as exception = server.getlasterror() 'construct error message to write to nt event log dim message as string = "url " & pageurl message = message & " error: " message = message & errorinfo.tostring 'nt event log name to write message to dim logname as string = "mycustomlog" 'create event log if it doesn’t exist if (not eventlog.sourceexists(logname)) then eventlog.createeventsource(logname, logname) end if 'fire off to event log dim log as new eventlog log.source = logname log.writeentry(message, eventlogentrytype.error) end sub </script>