如何在ASP.NET MVC中记录未处理的异常?

时间:2021-03-20 04:12:24

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

这是我在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:

但是Server.Transfer失败了:

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

子请求“http://www.example.com/error.html”的路径无效。预计会有虚拟路径。

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

我该如何解决?或者,对我来说这是一个更好的方法吗?

4 个解决方案

#1


9  

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.

我刚刚在Scott Hanselman的博客上发现了这个名为ELMAH的博客,它是一个错误记录器/处理程序,您无需更改代码即可使用。您可能想要研究它,因为它似乎与MVC很好地协作。

#2


3  

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.

ELMAH是一个很好的错误处理程序,用于捕获未处理的异常。它通过HttpModules无缝插入您的Web应用程序,并具有各种通知和日志记录选项。

Features:

特征:

  • SQL, XML, SQLite, InMemory Logging
  • SQL,XML,SQLite,InMemory日志记录
  • Email notification
  • 电子邮件通知
  • RSS feed
  • RSS订阅
  • 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

而且仅供参考,SO使用ELMAH,尽管是分叉版本。这是最好的架构解释和设置教程

#3


2  

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")

您必须指定一个虚拟路径,即相对于应用程序库的路径(即应用程序外部没有路径),所以类似于:Server.Transfer(“error.html”)或Server.Transfer(“/ error.html “)或Server.Transfer(”〜/ error.html“)

#4


2  

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

默认情况下,ASP.NET MVC应用程序具有继承自的Shared / Error.aspx视图

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.

如果您的控制器使用属性[HandleError],则所有异常将继续冒泡直到被捕获,并且它们将最终在该页面上。

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

我只是添加了一个内联的Page_Load(在这种情况下有效,因为它是行的结尾):

<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.

之后,友好的“抱歉......”消息。看起来ELMAH看起来更加强大,但是根据我的需要,这已经足够了。

#1


9  

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.

我刚刚在Scott Hanselman的博客上发现了这个名为ELMAH的博客,它是一个错误记录器/处理程序,您无需更改代码即可使用。您可能想要研究它,因为它似乎与MVC很好地协作。

#2


3  

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.

ELMAH是一个很好的错误处理程序,用于捕获未处理的异常。它通过HttpModules无缝插入您的Web应用程序,并具有各种通知和日志记录选项。

Features:

特征:

  • SQL, XML, SQLite, InMemory Logging
  • SQL,XML,SQLite,InMemory日志记录
  • Email notification
  • 电子邮件通知
  • RSS feed
  • RSS订阅
  • 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

而且仅供参考,SO使用ELMAH,尽管是分叉版本。这是最好的架构解释和设置教程

#3


2  

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")

您必须指定一个虚拟路径,即相对于应用程序库的路径(即应用程序外部没有路径),所以类似于:Server.Transfer(“error.html”)或Server.Transfer(“/ error.html “)或Server.Transfer(”〜/ error.html“)

#4


2  

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

默认情况下,ASP.NET MVC应用程序具有继承自的Shared / Error.aspx视图

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.

如果您的控制器使用属性[HandleError],则所有异常将继续冒泡直到被捕获,并且它们将最终在该页面上。

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

我只是添加了一个内联的Page_Load(在这种情况下有效,因为它是行的结尾):

<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.

之后,友好的“抱歉......”消息。看起来ELMAH看起来更加强大,但是根据我的需要,这已经足够了。