How do I log unhandled exceptions in ASP.NET MVC?

倖福魔咒の 提交于 2019-12-08 19:10:48

问题


Here's what I'm trying to do in my Global.asax.vb:

Public Class MvcApplication
    Inherits System.Web.HttpApplication

    Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
        routes.MapRoute( _
            "Error", _
            "error.html", _
            New With {.controller = "Error", _
                      .action = "FriendlyError"} _
        )
        ...
        'other routes go here'
        ...
    End Sub

    Sub Application_Start()
        RegisterRoutes(RouteTable.Routes)
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ...
        'code here logs the unhandled exception and sends an email alert'
        ...
        Server.Transfer("http://www.example.com/error.html")
    End Sub

End Class

But that Server.Transfer fails:

Invalid path for child request 'http://www.example.com/error.html'. A virtual path is expected.

How do I fix this? Or, what's a better way for me to do this?


回答1:


I just found this on Scott Hanselman's blog here called ELMAH which is an Error Logger/Handler that you can use without having to change your code. You might want to look into it, since it seems to work nicely with MVC.




回答2:


ELMAH is an excellent error handler for catching unhandled exceptions. It plugs seamlessly into your web application via HttpModules and has various options for notification and logging.

Features:

  • SQL, XML, SQLite, InMemory Logging
  • Email notification
  • RSS feed
  • Detailed! logging of exceptions
  • Easy integration
  • Error Signalling - signal the error handler of an error while "dieing nicely" for the user

And FYI, SO uses ELMAH, albeit a forked version. This is the best architectural explanation and setup tutorial




回答3:


You have to specifiy a virtual path, i.e. a path relative to the application base (i.e. no paths external to the app), so something like: Server.Transfer("error.html") or Server.Transfer("/error.html") or Server.Transfer("~/error.html")




回答4:


By default, ASP.NET MVC applications have a view Shared/Error.aspx, inheriting from

System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>

If your controller uses the attribute [HandleError], all exceptions will continue to bubble until caught, and they will end up on that page.

I simply added an inline Page_Load (valid in this case since it's the end of the line):

<script runat="server">
  Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
    MyExceptionHandlerService.LogException("exceptionsource", this.Model.Exception)
  End Sub
</script>

After it, the friendly "Sorry..." message. It definitely looks like ELMAH is more robust, but for my needs, this was sufficient.



来源:https://stackoverflow.com/questions/811958/how-do-i-log-unhandled-exceptions-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!