我如何连接/替换“最终用户”?耶索德?

发布于 2024-08-20 07:26:37 字数 1065 浏览 6 评论 0原文

我想在开发期间或在服务器本地提供有用信息时显示 YSOD,但在其他情况下显示半通用页面。我知道我可以在 web.config 中设置应用程序的 配置标记的 defaultRedirect 属性,但我宁愿做一些处理来生成一个稍微更好的信息。

我的所有控制器都继承自一个中央 BaseController 类,我在其中重写了 OnException (基本上像这样):

protected override void OnException(ExceptionContext filterContext) {
    //if something really bad happened and we get inside this if, 
    //just let the YSOD appear because there isn't anything we can do
    if (filterContext == null)
        return; 

    LogException(filterContext.Exception);

    //insert answer for question here:
    if (FigureOutIfDetailedYsodWouldBeDisplayed(filterContext)) 
        return;

    //what to actually do for end users
    filterContext.ExceptionHandled = true;
    filterContext.Result = View("ErrorPage", GetErrorModel(filterContext));
}

我应该如何实现 FigureOutIfDetailedYsodWouldBeDisplayed (答案不需要是代码,指向正确方向的指针就可以了)? 我当前的实现会检查原始 url 是否存在“//localhost”,但这个解决方案感觉很笨拙并且不能一直工作(例如,如果开发人员有一个主机条目来输入 localhost 以外的内容:这是我们的要求应用程序曾经有)。

I want to display the YSOD when it would be providing useful information during development or locally on the servers but a semi-generic page in other cases. I know that I could set the defaultRedirect attribute of the application's <customErrors> configuration tag in web.config, but I would rather do some processing to generate a page with slightly better information.

All of my controllers inherit from one central BaseController class where I have overridden OnException (essentially like this):

protected override void OnException(ExceptionContext filterContext) {
    //if something really bad happened and we get inside this if, 
    //just let the YSOD appear because there isn't anything we can do
    if (filterContext == null)
        return; 

    LogException(filterContext.Exception);

    //insert answer for question here:
    if (FigureOutIfDetailedYsodWouldBeDisplayed(filterContext)) 
        return;

    //what to actually do for end users
    filterContext.ExceptionHandled = true;
    filterContext.Result = View("ErrorPage", GetErrorModel(filterContext));
}

How should I implement FigureOutIfDetailedYsodWouldBeDisplayed (answer need not be code, a pointer in the right direction would be just fine)?
My current implementation checks the raw url for the existence of "//localhost", but this solution feels clumsy and doesn't work all the time (for example if the dev has a host entry to type something other than localhost: a requirement our app used to have).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两个我 2024-08-27 07:26:38

如果您实际上使用开发环境的“调试”设置和生产环境的“发布”设置进行构建,那么您始终可以这样做

#if DEBUG
    //Show YSOD
#else
    //Show friendly error page
#endif

If you actually build using the Debug setting for Dev environments, and Release for production environments, you could always just do

#if DEBUG
    //Show YSOD
#else
    //Show friendly error page
#endif
花开雨落又逢春i 2024-08-27 07:26:37

您是否知道您还可以为每个 HTTP 错误代码添加自定义错误页面?那么您真的需要这个自定义错误处理代码吗?

  <customErrors mode="RemoteOnly" defaultRedirect="Error/Default">
    <error statusCode="401" redirect="Error/AccessDenied" />
    <error statusCode="404" redirect="Error/NotFound" />
  </customErrors>

另外,除非您确实有特定的需求,否则我建议您不要自己编写此异常日志记录代码。帮自己一个忙,看看 ELMAH。严重地。 [免责声明:我没有参与这个项目,只是一个非常快乐的用户]

它所需要的只是(几乎!)你的 web.config 中的几行,在你的 bin 文件夹中放置一个 dll,并且(如果你想登录到DB)单个 .sql 脚本。

来自项目页面

“ELMAH(错误记录模块和处理程序)是一个应用程序范围的完全可插入的错误日志记录工具可以动态添加到正在运行的 ASP.NET Web 应用程序,甚至是计算机上的所有 ASP.NET Web 应用程序,无需重新编译或重新部署

ELMAH 。放入正在运行的 Web 应用程序并进行适当配置,您无需更改一行代码即可获得以下功能:

  • 记录几乎所有未处理的异常 一个
  • 查看已重新编码的异常的整个日志的网页。
  • 用于远程 在许多情况下,即使关闭了自定义错误
  • 您也可以查看 ASP.NET 生成的原始死机黄屏。
  • 模式, 它发生的时间。”

Did you know you can also add custom error pages per HTTP Error code? Do you really need this custom error handling code then?

  <customErrors mode="RemoteOnly" defaultRedirect="Error/Default">
    <error statusCode="401" redirect="Error/AccessDenied" />
    <error statusCode="404" redirect="Error/NotFound" />
  </customErrors>

Also, unless you have really specific needs, I would recommend not writing this exception logging code yourself. Do yourself a favor and check out ELMAH. Seriously. [disclaimer: I am not involved in this project, just a very happy user]

All it takes is (litterally!) a few lines in your web.config, dropping a dll in your bin folder, and (if you want to log to a DB) a single .sql script.

From the project page:

"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.
  • 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."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文