惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
博客园 - 司徒正美
P
Privacy International News Feed
G
Google Developers Blog
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
K
Kaspersky official blog
I
InfoQ
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
量子位
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
S
Security Affairs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
S
Security Archives - TechRepublic
V
Visual Studio Blog
T
Troy Hunt's Blog
S
Secure Thoughts
F
Fortinet All Blogs
V
V2EX
The Register - Security
The Register - Security
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - 王庆

crosstool-ng搭建交叉编译环境注意事项 多重继承及虚继承中对象内存的分布 编写和调试Android下JNI程序流程 eclipse xml文件中按alt+/没有提示信息 Android NDK 工具链的使用方法(Standalone Toolchain) 编译Android VNC Server 使用gdb和gdbserver调试Android C/C++程序 openssl在多平台和多语言之间进行RSA加解密注意事项 LINUX开发使用的3个远程工具 NDK 链接第三方静态库的方法 动态生成程序集和类型 关于线程池的一段代码 理解ParseChildren用法 快速查找ASP.NET产生的临时文件 Synchronized vs SyncRoot Exception vs ApplicationException 【转】Hashtable,ListDictionary,HybridDictionary比较 Monitor用法 C#多线程之Thread
捕获ASP.NET程序发生的异常
王庆 · 2008-07-14 · via 博客园 - 王庆

捕获ASP.NET异常,这种文章在网上已经屡见不鲜了,在这之前,我也看了不少别人写的代码,学了别人不少东西。在别人的基础上,我添加了一些自己写的东西,现在贴出来,共享一下,希望能对大家有点帮助。

 1using System.Text;
 2using System.Web;
 3using System.Configuration;
 4using System.IO;
 5
 6namespace HNRInfo.Framework.HttpModule
 7{
 8    /// <summary>
 9    /// 捕获未处理的系统的错误,将错误添加的数据库中,同时指向一个友好的错误页面
10    /// </summary>

11    public class CatchError : IHttpModule
12    {
13        private static string sysErrorPage = ConfigurationManager.AppSettings["SysErrorPage"].ToString();
14
15        public void Dispose()
16        {
17        }

18
19        public void Init(HttpApplication context)
20        {
21            context.Error += new EventHandler(SaveError);
22        }

23        //将捕获的错误信息插入数据库
24        private void SaveError(object sender, EventArgs e)
25        {
26            HttpContext context = ((HttpApplication)sender).Context;
27            string path = context.Request.RawUrl;
28            string hostIP = context.Request.UserHostAddress;
29
30            string title = string.Empty;
31            string info = string.Empty;
32
33            GetLastError(context.Server.GetLastError(), ref title, ref info);
34
35            HNRInfo.Model.SystemError errorModel = new HNRInfo.Model.SystemError();
36            errorModel.errorTitle = title;
37            errorModel.errorInfo = info;
38            errorModel.occurUrl = path;
39            errorModel.hostIP = hostIP;
40
41            HNRInfo.DALFactory.PlatFactory.SystemError.Add(errorModel);
42
43            //重定向到友好的错误页面
44            context.Server.Transfer(sysErrorPage, false);
45        }

46
47        /// <summary>
48        /// 找到导致异常的最初错误
49        /// </summary>

50        private void GetLastError(Exception e, ref string title, ref string info)
51        {
52            if (e.InnerException != null)
53                GetLastError(e.InnerException, ref title, ref info);
54            else
55            {
56                title = e.Message;
57                info = e.StackTrace;
58            }

59        }

60    }

61}

62

35-41行,是存入数据库的一些操作,这几行代码,不用理会。由于这段代码的在一个名为HNRInfo.Framework的程序集中,故web.config中的配置为:

      <httpModules>
        
<!--捕获错误-->
        
<add type="HNRInfo.Framework.HttpModule.CatchError, HNRInfo.Framework" name="HNRInfo.Framework" />
      
</httpModules>

代码没有什么,挺简单的,简单是因为.NET在后台已经为我们做了很多复杂的工作。我觉得大家如果有时间的话,可以看看这个StackTrace类型的用法,下边列出一些朋友的文章(关于StackTrace的用法):
浅析StackTrace
使用 StackTrace 获得更多跟 Exception 有关的信息
用System.Diagnostices.StackTrace取得呼叫堆疊資訊。