简体   繁体   English

抛出异常并通知用户

[英]Throwing exceptions and notifying the user

I've recently joined an asp.net mvc project well under way where there isnt much consistency in dealing with exceptions in the controller;我最近加入了一个正在进行中的 asp.net mvc 项目,该项目在处理 controller 中的异常方面没有太多一致性; some devs return data to the client to let the user know whats wrong, others throw them back so they get to the server-level handler that processes and logs them - without letting the user know whats up.一些开发人员将数据返回给客户端,让用户知道出了什么问题,而另一些开发人员则将数据返回给客户端,以便他们到达处理和记录它们的服务器级处理程序——而不让用户知道发生了什么。

It seems obvious to me that both approaches are wrong on their own, and need to complement each other instead;在我看来很明显,这两种方法本身都是错误的,需要相互补充; what I'm stuck at, is how to do that.我坚持的是如何做到这一点。 I assume the eventual exception handler / logger could redirect the user to an error webpage upon catching something particularly nasty, but that limits the mechanism to just severe stuff.我假设最终的异常处理程序/记录器可以在捕获到特别讨厌的东西时将用户重定向到错误网页,但这将机制限制在严重的东西上。

I'm kind of looking for a way to do both "throw" and "return..." at a time when I catch an exception, so I get it sorted and logged server side and get data client side that lets me tell the user there's been a hiccup.我正在寻找一种方法,在我捕获到异常时同时执行“抛出”和“返回...”,所以我将它排序并记录在服务器端并获取数据客户端,让我告诉用户出现问题。

My expertise with asp.net is very limited, and while I believe I understand mvc enough for it to not be an issue, this is kind of a "what is the best practice?"我在 asp.net 方面的专业知识非常有限,虽然我相信我对 mvc 的理解足以让它不会成为问题,但这有点像“什么是最佳实践?” question from someone working with people who dont bother with best practices much.来自与不太关心最佳实践的人一起工作的人的问题。

There is a good project called Elmah for logging errors and exceptions in ASP.NET applications.有一个名为 Elmah 的好项目,用于记录 ASP.NET 应用程序中的错误和异常。 You can find it here你可以在这里找到它

ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. ELMAH(错误记录模块和处理程序)是一个应用程序范围的错误记录工具,完全可插入。 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.可以动态添加到一个正在运行的ASP.NET web应用,甚至是一台机器上的所有ASP.NET web应用,不需要重新编译或者重新部署。

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:将 ELMAH 放入正在运行的 web 应用程序并进行适当配置后,您无需更改一行代码即可获得以下功能:

  • Logging of nearly all unhandled exceptions.记录几乎所有未处理的异常。
  • A web page to remotely view the entire log of recoded exceptions.一个 web 页面,用于远程查看重新编码的异常的整个日志。
  • A web page to remotely view the full details of any one logged exception, including colored stack traces.一个 web 页面,用于远程查看任何一个记录的异常的完整详细信息,包括彩色堆栈跟踪。
  • 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.在许多情况下,您可以查看 ASP.NET 针对给定异常生成的原始黄屏死机,即使 customErrors 模式已关闭。
  • An e-mail notification of each error at the time it occurs.每个错误发生时的电子邮件通知。
  • An RSS feed of the last 15 errors from the log.日志中最后 15 个错误的 RSS 提要。

The MVC application that I am working on implements the Application_Error in the Global.asax to handle exceptions thrown from the application and then redirects the user to a standard error page.我正在处理的 MVC 应用程序在Global.asax中实现Application_Error以处理应用程序抛出的异常,然后将用户重定向到标准错误页面。 The Controller for the error page handles the logging as well as displays just enough information about the error to allow a support person to find their session in the system and help resolve any issues.错误页面的 Controller 处理日志记录并显示有关错误的足够信息,以允许支持人员在系统中找到他们的 session 并帮助解决任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM