本文介绍了ASP.NET MVC错误日志记录Global.asax和Error.aspx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个ASP.NET MVC应用程序。我需要在两个地方处理异常。



Global.asax.vb文件:

 code>公共类MvcApplication 
继承System.Web.HttpApplication
...
Sub Application_Error(ByVal sender As Object,ByVal e As EventArgs)
LogException(HttpContext。 Current.Server.GetLastError(),Request)
End Sub
Shared Sub LogException(ByVal ex As Exception,ByRef r As System.Web.HttpRequest)
...
结束Sub
结束类

Views\Shared\Error.aspx文件:

 <%@ Page Language =VBInherits =System.Web.Mvc.ViewPage(Of System.Web.Mvc.HandleErrorInfo )%> 
< script runat =server>
Sub Page_Load(ByVal Sender As System.Object,ByVal e As System.EventArgs)
MvcApplication.LogException(Model.Exception,Request)
End Sub
< / script>
...

但是我收到这个错误:

我应该在哪里定义我的LogException()函数,以便它可以从Global.asax.vb文件和错误访问。 aspx文件?最多的MVC-ish在哪里?

解决方案

您可能想尝试一个名为ELMAH的模块。 Scott Hanselman说:ELMAH是您的ASP.NET错误的Tivo。我目前正在使用它来记录我的ASP.NET + MVC应用程序中的错误,它的作用就像一个魅力。安装很容易,因为所有这些都是添加到web.config的行。您甚至可以限制谁有权限查看错误日志。




I'm creating an ASP.NET MVC application. I need to handle exceptions in two places.

Global.asax.vb file:

Public Class MvcApplication
    Inherits System.Web.HttpApplication
    ...
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        LogException(HttpContext.Current.Server.GetLastError(), Request)
    End Sub
    Shared Sub LogException(ByVal ex As Exception, ByRef r As System.Web.HttpRequest)
        ...
    End Sub
End Class

Views\Shared\Error.aspx file:

<%@ Page Language="VB" Inherits="System.Web.Mvc.ViewPage(Of System.Web.Mvc.HandleErrorInfo)" %>
<script runat="server">
    Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
        MvcApplication.LogException(Model.Exception, Request)
    End Sub
</script>
...

But I get this error:

Where should I define my LogException() function so that it is accessible from both the Global.asax.vb file and the Error.aspx file? Where is the most MVC-ish?

解决方案

You may want to try an module called ELMAH. Scott Hanselman says "ELMAH is Tivo for your ASP.NET Errors". I've am currently using it for logging errors in my ASP .NET + MVC applications and it works like a charm. Setup was easy because all that is required is adding lines to the web.config. You can even restrict who has access to view the error logs.

http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx

这篇关于ASP.NET MVC错误日志记录Global.asax和Error.aspx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:24
查看更多