如何获得ELMAH与ASP.NET MVC [的HandleError]属性的工作吗? [英] How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?

查看:157
本文介绍了如何获得ELMAH与ASP.NET MVC [的HandleError]属性的工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用ELMAH在我的ASP.NET MVC应用程序记录错误,但是当我使用[的HandleError]属性我控制器发生时ELMAH不会记录任何错误。

I am trying to use ELMAH to log errors in my ASP.NET MVC application, however when I use the [HandleError] attribute on my controllers ELMAH doesn't log any errors when they occur.

由于我猜测其因为ELMAH只记录未处理的错误和[的HandleError]属性处理错误,以便从而无需登录它。

As I am guessing its because ELMAH only logs unhandled errors and the [HandleError] attribute is handling the error so thus no need to log it.

如何修改或我将如何去有关修改属性,所以ELMAH可以知道有一个错误并记录它。

How do I modify or how would I go about modifying the attribute so ELMAH can know that there was an error and log it..

编辑:我要确保每个人都明白,我知道我可以修改属性,那不是我要问的问题... ELMAH被使用的HandleError属性,这意味着它的时候绕过不会看到有是因为它是由属性已经处理的错误...我所问的是有没有办法让ELMAH看到错误并记录它,即使该属性来处理它...我搜索周围,不看到任何方法调用,迫使它记录错误....

Let me make sure everyone understands, I know I can modify the attribute thats not the question I'm asking... ELMAH gets bypassed when using the handleerror attribute meaning it won't see that there was an error because it was handled already by the attribute... What I am asking is there a way to make ELMAH see the error and log it even though the attribute handled it...I searched around and don't see any methods to call to force it to log the error....

推荐答案

您也可以继承<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute.aspx\"><$c$c>HandleErrorAttribute并覆盖其<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute.onexception.aspx\"><$c$c>OnException构件(无需复制),使得它记录与ELMAH且仅当基实现处理它的异常。你需要code的最小量如下:

You can subclass HandleErrorAttribute and override its OnException member (no need to copy) so that it logs the exception with ELMAH and only if the base implementation handles it. The minimal amount of code you need is as follows:

using System.Web.Mvc;
using Elmah;

public class HandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute
{
    public override void OnException(ExceptionContext context)
    {
        base.OnException(context);
        if (!context.ExceptionHandled) 
            return;
        var httpContext = context.HttpContext.ApplicationInstance.Context;
        var signal = ErrorSignal.FromContext(httpContext);
        signal.Raise(context.Exception, httpContext);
    }
}

基本实现第一次调用,给它一个机会,以纪念异常作为被处理。只有这样,异常信号。以上code是简单的,如果在一个环境中使用可能会导致问题,其中的HttpContext 可能不可用,如测试。这样一来,你会希望code,它是较为抗跌(在作为成本稍长):

The base implementation is invoked first, giving it a chance to mark the exception as being handled. Only then is the exception signaled. The above code is simple and may cause issues if used in an environment where the HttpContext may not be available, such as testing. As a result, you will want code that is that is more defensive (at the cost of being slightly longer):

using System.Web;
using System.Web.Mvc;
using Elmah;

public class HandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute
{
    public override void OnException(ExceptionContext context)
    {
        base.OnException(context);
        if (!context.ExceptionHandled       // if unhandled, will be logged anyhow
            || TryRaiseErrorSignal(context) // prefer signaling, if possible
            || IsFiltered(context))         // filtered?
            return;

        LogException(context);
    }

    private static bool TryRaiseErrorSignal(ExceptionContext context)
    {
        var httpContext = GetHttpContextImpl(context.HttpContext);
        if (httpContext == null)
            return false;
        var signal = ErrorSignal.FromContext(httpContext);
        if (signal == null)
            return false;
        signal.Raise(context.Exception, httpContext);
        return true;
    }

    private static bool IsFiltered(ExceptionContext context)
    {
        var config = context.HttpContext.GetSection("elmah/errorFilter")
                        as ErrorFilterConfiguration;

        if (config == null)
            return false;

        var testContext = new ErrorFilterModule.AssertionHelperContext(
                              context.Exception, 
                              GetHttpContextImpl(context.HttpContext));
        return config.Assertion.Test(testContext);
    }

    private static void LogException(ExceptionContext context)
    {
        var httpContext = GetHttpContextImpl(context.HttpContext);
        var error = new Error(context.Exception, httpContext);
        ErrorLog.GetDefault(httpContext).Log(error);
    }

    private static HttpContext GetHttpContextImpl(HttpContextBase context)
    {
        return context.ApplicationInstance.Context;
    }
}

这第二个版本将首先尝试使用从错误信号ELMAH ,这涉及到完全配置管道日志记录,邮件,过滤和你有什么。如果做不到这一点,它会尝试看看是否应该过滤的错误。如果不是,则简单地记录错误。此实现不处理邮件通知。如果异常,可随后信号如果配置这样做,邮件将被发送。

This second version will try to use error signaling from ELMAH first, which involves the fully configured pipeline like logging, mailing, filtering and what have you. Failing that, it attempts to see whether the error should be filtered. If not, the error is simply logged. This implementation does not handle mail notifications. If the exception can be signaled then a mail will be sent if configured to do so.

您可能还需要照顾,如果多个 HandleErrorAttribute 实例生效然后复制记录不会出现,但上面的两个例子应该让你开始。

You may also have to take care that if multiple HandleErrorAttribute instances are in effect then duplicate logging does not occur, but the above two examples should get your started.

这篇关于如何获得ELMAH与ASP.NET MVC [的HandleError]属性的工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆