在 C#.NET 中捕获所有未处理异常的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/295731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easy way to catch all unhandled exceptions in C#.NET
提问by adambox
I have a website built in C#.NET that tends to produce a fairly steady stream of SQL timeouts from various user controls and I want to easily pop some code in to catch all unhandled exceptions and send them to something that can log them and display a friendly message to the user.
我有一个用 C#.NET 构建的网站,它倾向于从各种用户控件中产生相当稳定的 SQL 超时流,我想轻松地弹出一些代码来捕获所有未处理的异常并将它们发送到可以记录它们并显示一个给用户的友好信息。
How do I, through minimal effort, catch all unhandled exceptions?
我如何通过最少的努力捕获所有未处理的异常?
this questionseems to say it's impossible, but that doesn't make sense to me (and it's about .NET 1.1 in windows apps):
这个问题似乎说这是不可能的,但这对我来说没有意义(它是关于 Windows 应用程序中的 .NET 1.1):
回答by Jon Skeet
Do you mean handling it in all threads, including ones created by third-party code? Within "known" threads just catch Exception
at the top of the stack.
你的意思是在所有线程中处理它,包括由第三方代码创建的线程?在“已知”线程中,只Exception
在堆栈顶部捕获。
回答by Ali Ers?z
All unhandled exceptions finally passed through Application_Error in global.asax. So, to give general exception message or do logging operations, see Application_Error.
所有未处理的异常最终都通过 global.asax 中的 Application_Error。因此,要给出一般异常消息或进行日志记录操作,请参阅Application_Error。
回答by harriyott
回答by MichaelT
回答by P Daddy
You can subscribe to the AppDomain.CurrentDomain.UnhandledException
event.
您可以订阅该AppDomain.CurrentDomain.UnhandledException
事件。
回答by Nathan Prather
If using .net 2.0 framework, I use the built in Health Monitoring services. There's a nice article describing this method here: http://aspnet.4guysfromrolla.com/articles/031407-1.aspx
如果使用 .net 2.0 框架,我使用内置的健康监控服务。这里有一篇很好的文章描述了这种方法:http: //aspnet.4guysfromrolla.com/articles/031407-1.aspx
If you're stuck with the 1.0 framework, I would use ELMAH: http://msdn.microsoft.com/en-us/library/aa479332.aspx
如果您坚持使用 1.0 框架,我会使用 ELMAH:http: //msdn.microsoft.com/en-us/library/aa479332.aspx
hope this helps
希望这可以帮助
回答by user35559
There are 2 parts to this problem handling & identifying.
这个问题处理和识别有两个部分。
Identifying
识别
This is what you do when the exception is finally caught, not necessarily where it is thrown. So the exception at that stage must have enough context information for you to idenitfy what the problem was
这就是最终捕获异常时所做的事情,不一定是抛出异常的地方。所以那个阶段的异常必须有足够的上下文信息让你识别问题是什么
Handling
处理
For handling, you can a) add a HttpModeule. See http://www.eggheadcafe.com/articles/20060305.aspI would suggest this approach only when there is absolutely no context informaatn available and there might be issuus wiih IIS/aspnet, In short for catastrophic situations
对于处理,您可以 a) 添加一个 HttpModeule。请参阅 http://www.eggheadcafe.com/articles/20060305.asp我建议只有在绝对没有可用的上下文信息并且可能存在 IIS/aspnet 问题时才建议使用这种方法,简而言之是灾难性的情况
b) Create a abstract class called AbstractBasePage which derives from Page class and have all your codebehind classes derive from AbstractBasePage
b) 创建一个名为 AbstractBasePage 的抽象类,它派生自 Page 类,并使所有代码隐藏类派生自 AbstractBasePage
The AbstractBasePage can implement that Page.Error delegate so that all exceptions which percolate up through the n-tier architecture can be caught here(and possibly logged)
AbstractBasePage 可以实现该 Page.Error 委托,以便所有通过 n 层架构向上渗透的异常都可以在这里捕获(并可能记录)
I would suggest this cause for the kind of exceptions you are talking about (SQlException) there is enough context information for you to identify that it was a timeout and take possible action. This action might include redirecting user to a custom error page with appropriate message for each different kind of exception (Sql, webservice, async call timeouts etc).
对于您正在谈论的异常类型(SQlException),我建议您使用这种原因,有足够的上下文信息供您识别它是超时并采取可能的措施。此操作可能包括将用户重定向到带有针对每种不同类型异常(Sql、Web 服务、异步调用超时等)的适当消息的自定义错误页面。
Thanks RVZ
谢谢 RVZ
回答by Dan Powley
Use the Application_Error method in your Global.asax file. Inside your Application_Error method implementation call Server.GetLastError(), log the details of the exception returned by Server.GetLastError() however you wish.
在 Global.asax 文件中使用 Application_Error 方法。在您的 Application_Error 方法实现调用 Server.GetLastError() 中,根据需要记录 Server.GetLastError() 返回的异常的详细信息。
e.g.
例如
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
log4net.ILog log = log4net.LogManager.GetLogger(typeof(object));
using (log4net.NDC.Push(this.User.Identity.Name))
{
log.Fatal("Unhandled Exception", Server.GetLastError());
}
}
Don't pay too much attention to the log4net stuff, Server.GetLastError() is the most useful bit, log the details however you prefer.
不要过多关注 log4net 的内容,Server.GetLastError() 是最有用的部分,您可以根据自己的喜好记录详细信息。
回答by Dan Powley
The ELMAH projectsounds worth a try, its list of features include:
该ELMAH项目的声音值得一试,它的功能列表包括:
ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.
- Logging of nearly all unhandled exceptions.
- A web page to remotely view the entire log of recoded exceptions.
- A web page to remotely view the full details of any one logged exception.
- In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with customErrors mode turned off.
- An e-mail notification of each error at the time it occurs.
- An RSS feed of the last 15 errors from the log.
- A number of backing storage implementations for the log
ELMAH(错误日志模块和处理程序)是一个应用程序范围的错误日志工具,它是完全可插入的。它可以动态添加到正在运行的 ASP.NET Web 应用程序,甚至是机器上的所有 ASP.NET Web 应用程序,无需重新编译或重新部署。
- 记录几乎所有未处理的异常。
- 用于远程查看记录异常的整个日志的网页。
- 用于远程查看任何记录异常的完整详细信息的网页。
- 在许多情况下,您可以查看 ASP.NET 为给定异常生成的原始黄屏死机,即使关闭了 customErrors 模式。
- 每个错误发生时的电子邮件通知。
- 日志中最后 15 个错误的 RSS 提要。
- 日志的一些后备存储实现
More on using ELMAH from dotnetslackers
更多关于使用来自dotnetslackers 的 ELMAH
回答by Adam Tegen
It's probably important to note that you are not supposedto catch unhandled exceptions. If you are having SQL timeout issues, you should specifically catch those.
需要注意的是,您不应该捕获未处理的异常。如果您遇到 SQL 超时问题,您应该专门解决这些问题。