我最近加入了一个正在进行的 asp.net mvc 项目,该项目在 Controller 中处理异常方面没有太多的一致性;一些开发人员将数据返回给客户端,让用户知道出了什么问题,而另一些开发人员则将数据返回给客户端,以便他们到达处理和记录它们的服务器级处理程序——而不让用户知道发生了什么。
在我看来很明显,这两种方法本身都是错误的,需要相互补充;我坚持的是如何做到这一点。我假设最终的异常处理程序/记录器可以在捕获到特别讨厌的东西时将用户重定向到错误网页,但这将机制限制为仅处理严重的东西。
我正在寻找一种方法,在我捕获到异常时同时执行“抛出”和“返回...”,所以我在服务器端对其进行排序和记录,并获取数据客户端,让我告诉用户出现了问题。
我在 asp.net 方面的专业知识非常有限,虽然我相信我对 mvc 的了解足以让它成为问题,但这有点像“什么是最佳实践?”来自与不太关心最佳实践的人一起工作的人的问题。
最佳答案
有一个名为 Elmah 的好项目,用于记录 ASP.NET 应用程序中的错误和异常。你可以找到它here
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.
Once ELMAH has been dropped into a running web application and configured appropriately, you get the following facilities without changing a single line of your code:
- 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, including colored stack traces.
- 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.
关于c# - 抛出异常并通知用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10088406/