zoukankan      html  css  js  c++  java
  • 在系统出现未处理的错误时,在Global的Application_Error记录下错误

            在我们开发系统时,一般都会记录日志信息,这样方便日后进行维护,同时如果系统出现了错误,也会方便查找,很多

    系统开发时都会使用成熟的日志组件,如log4net。但是我今天要介绍的不是日志组件,而是在某些特别的情况下,我们没有

    能捕获错误该怎么办???

            正如标题所说的,我们可以在Global文件的Application_Error中对错误进行捕获,并记录下来。

    下面就来看看下面一段示例代码:

    protected void Application_Error(object sender, EventArgs e)
            {
                // 在出现未处理的错误时运行,获取错误
                Exception objErr = Server.GetLastError().GetBaseException();
                if (objErr != null)
                {
                    string error = "Error Page: " + Request.Url.ToString() + "<br>";
                    if (objErr.Message != null)
                    {
                        error += "Error Message: " + objErr.Message + "<br>";
                    }
                    if (objErr.StackTrace != null)
                    {
                        error += "Stack Message:" + objErr.StackTrace + "<br>";
                    }
                    if (objErr.InnerException != null)
                    {
                        error += "InnerException" + objErr.InnerException.Message + "<br>";
                    }
                    string strSystemLog = "GlobalSystemLog.Log";
                    string strSystemLogpath = Server.HtmlEncode(strSystemLog);
                    FileInfo fi = new FileInfo(strSystemLogpath);
                    if (File.Exists(strSystemLogpath))
                    {
                        using (StreamWriter sw = fi.AppendText())
                        {
                            sw.WriteLine();
                            sw.WriteLine(DateTime.Now.ToShortTimeString() + "\n" + error + "\n");
                            sw.Close();
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = fi.CreateText())
                        {
                            sw.WriteLine();
                            sw.WriteLine(DateTime.Now.ToShortTimeString() + "\n" + error + "\n");
                            sw.Close();
                        }
                    }

                    Server.ClearError();
                    Application["error"] = error;

                    //跳转到系统出错页面
                    Response.Redirect("~/SystemError.aspx");
                }
            }

  • 相关阅读:
    触摸屏多媒体查询展示自主设计系统开发过程
    hashtable数据循环读取的顺序问题
    vs2010英文版打包中文框架出错的解决办法
    Silverlight游戏开发初探(上篇)
    PB之——编码规范
    时间相加 ,使用SQL完成
    PB(POWERBUILDER) 基础介绍
    PB之——流程控制
    PB之——基本数据类型
    PB [Grid风格数据窗口改变线条颜色] 的变通实现方法(也可以成为 带表头的Grid数据窗口)
  • 原文地址:https://www.cnblogs.com/kevinGao/p/2323342.html
Copyright © 2011-2022 走看看