使用customErrors mode =“on”时,如何在asp.net mvc 3中记录没有控制器的异常?

发布于 2024-10-10 01:53:53 字数 730 浏览 0 评论 0原文

我今天读了很多关于在 ASP.NET MVC 中实现自定义错误页面(使用 3 RC2)主题的材料。我想做的一件事是记录异常,但我不确定最好的挂钩在哪里。

我主要遵循 Barry Dahlberg 的建议,这帮助我设置了所需的404行为。我想将它们与服务器错误分开,并以不同的方式记录它们。

似乎当我有时,MVC3 仅使用 ~/Views/Shared/Error.cshtml 中定义的视图,传递正确的 System.Web.Mvc.HandleErrorInfo 模型实例。实际上,这绕过了我放置错误日志代码的控制器。

我尝试设置,但控制器操作方法永远不会被调用。

视图中的 HandleErrorInfo 模型具有我记录日志所需的异常信息。我可以在视图中记录错误,但这听起来很有趣。

另一个解决方案是保留。由于这会导致从 Global.asax 调用 Application_Error,因此我可以在那里记录。

有想法吗?

I've read a lot of material today on the topic of implementing custom error pages in ASP.NET MVC (using 3 RC2). One thing I want to be able to do is log exceptions, but I'm not sure where the best hook is.

I mainly followed Barry Dahlberg's advice which helped me set up the desired 404 behaviors. I want to keep those separate from server errors, and log them differently.

It seems that when I have <customErrors mode="On">, MVC3 just uses the view defined at ~/Views/Shared/Error.cshtml, passing the correct System.Web.Mvc.HandleErrorInfo model instance. In effect, this is bypassing the controller where I would put the error logging code.

I tried setting <customErrors mode="On" defaultRedirect="errors/unexpected" />, but the controller action method never gets invoked.

The HandleErrorInfo model in the view has the exception information I need for logging. I could log the error in the view, but that smells funny.

Another solution would be to keep <customErrors mode="Off" />. Since this causes Application_Error to be invoked from Global.asax, I could log there instead.

Ideas?

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

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

发布评论

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

评论(1

_失温 2024-10-17 01:53:53

您可以编写一个 自定义 HandleError 属性,其中将在覆盖的 OnException 方法。

You could write a custom HandleError attribute where you would log the exception in the overriden OnException method.

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