asp.net中使用Global.asax文件中添加应用出错代码,写入系统日志文件或数据库

    void Application_Error(object sender, EventArgs e)
    {
        // 在出现未处理的错误时运行的代码
        Exception objErr = Server.GetLastError().GetBaseException(); //获取错误
        string err = "Error Caught in Application_Error event\n" +
        "Error in:" + Request.Url.ToString() +
        "\nError Message:" + objErr.Message.ToString() +
        "\nStack Trace:" + objErr.StackTrace.ToString();
        //将捕获的错误写入windows的应用程序日志中,可从事件查看器中访问应用程序日志。
        System.Diagnostics.EventLog.WriteEntry("Test2", err, System.Diagnostics.EventLogEntryType.Error);
        Server.ClearError(); //清除异常,其他地方不再捕获此异常。
    }

posted @ 2014-05-08 14:59  深南大道  阅读(351)  评论(0编辑  收藏  举报