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

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

原文地址:https://www.cnblogs.com/smartsmile/p/6234200.html