使用customErrors mode =“on”时,如何在asp.net mvc 3中记录没有控制器的异常?
我今天读了很多关于在 ASP.NET MVC 中实现自定义错误页面(使用 3 RC2)主题的材料。我想做的一件事是记录异常,但我不确定最好的挂钩在哪里。
我主要遵循 Barry Dahlberg 的建议,这帮助我设置了所需的404行为。我想将它们与服务器错误分开,并以不同的方式记录它们。
似乎当我有
我尝试设置
视图中的 HandleErrorInfo 模型具有我记录日志所需的异常信息。我可以在视图中记录错误,但这听起来很有趣。
另一个解决方案是保留
有想法吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以编写一个 自定义 HandleError 属性,其中将在覆盖的 OnException 方法。
You could write a custom HandleError attribute where you would log the exception in the overriden OnException method.