• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

asp.net中当服务器出错时显示指定的错误页面,同时把错误信息写入系统日志文件的探讨

发布: 2007-6-30 23:38 | 作者: admin | 来源: | 查看: 28次 | 进入软件测试论坛讨论

领测软件测试网 asp.net中当服务器出错时显示指定的错误页面同时把错误信息写入系统日志文件的探讨

一,在Web.config中填写出错时显示的页面,可以根据不同的statusCode显示不同的出错页面。
   <customErrors mode="On"  //如果设置为Off则出错只返回错误信息,不会跳到自己的指定页面defaultRedirect="/error/customerrorpage.aspx">
    <error statusCode="404" redirect="/error/404Page.aspx"/>
    <error statusCode="403" redirect="/error/403page.aspx"/>
  </customErrors>

二,在Global.asax文件中添加应用出错代码,写入系统日志文件
protected void Application_Error(Object sender, EventArgs e)
        {
            Exception LastError = Server.GetLastError();
            String ErrMessage = LastError.ToString();

            String LogName  = "MyLog";
            String Message = "Url " + Request.Path + " Error: " + ErrMessage;

            // Create Event Log if It Doesn@#t Exist
        
            if (!EventLog.SourceExists(LogName))
            {
                EventLog.CreateEventSource(LogName, LogName);
            }
            EventLog Log = new EventLog();
            Log.Source  = LogName;
            //These are the five options that will display a different icon.
            Log.WriteEntry(Message, EventLogEntryType.Information, 1);
            Log.WriteEntry(Message, EventLogEntryType.Error, 2);
            Log.WriteEntry(Message, EventLogEntryType.Warning, 3);
            Log.WriteEntry(Message, EventLogEntryType.SuccessAudit, 4);
            Log.WriteEntry(Message, EventLogEntryType.FailureAudit, 5);

        }
三,现在你可以进行测试了。
我在Default.aspx.cs中产生一个错误,果然跳到默认的错误页面!
private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            try
            {
                int y=0;
                int x=1/y;
            }
            catch (Exception Err)
            {
                throw new Exception("404");//我想产生不同的错误,对应web.config中的statusCode,该如何实现?
                //Err.
            }


延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网