Frage

I have an ASP.NET MVC site running on .NET 4.0 which I am trying to set up error logging.

I discovered the Elmah.MVC NuGet package (v2.1.1, Elmah core: v1.2.1) and followed this tutorial to get it setup. (did not do Step5 - javascript error logging)

It is working correctly and sending me emails and logging 404 errors, but when I enter some html into an input <h1>Test</h1> to see how the site handles it I get a HttpRequestValidationException which is good in the sense that it wont let them enter it and I have an error page setup which gets displayed when the site has been published, but Elmah does not log the details of this kind of error.

I have looked at this SO post and the Elmah issue 217 says that it has been resolved.

This is my ElmahHandleErrorAttribute class:

public class ElmahHandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute
{
    public override void OnException(ExceptionContext context)
    {
        base.OnException(context);

        var e = context.Exception;

// Log only handled exceptions, because all other will be caught by ELMAH anyway.
// from http://ivanz.com/2011/05/08/asp-net-mvc-magical-error-logging-with-elmah/
// did nothing
        // if (context.ExceptionHandled)
        //    ErrorSignal.FromCurrentContext().Raise(context.Exception);

        if (!context.ExceptionHandled   // if unhandled, will be logged anyhow
            || RaiseErrorSignal(e)      // prefer signaling, if possible
            || IsFiltered(context))     // filtered?
            return;

        LogException(e);
    }

    private static bool RaiseErrorSignal(Exception e)
    {
        var context = HttpContext.Current;
        if (context == null)
            return false;
        var signal = ErrorSignal.FromContext(context);
        if (signal == null)
            return false;
        signal.Raise(e, context);
        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, HttpContext.Current);

        return config.Assertion.Test(testContext);
    }

    private static void LogException(Exception e)
    {
        var context = HttpContext.Current;
        ErrorLog.GetDefault(context).Log(new Error(e, context));
    }
}

Which I have referenced in my FilterConfig.cs:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new ElmahHandleErrorAttribute());
    filters.Add(new HandleErrorAttribute());

}

Is there a known workaround so that I can make Elmah log and email details of these kinds of errors?

thanks.

War es hilfreich?

Lösung

This issue seems like about a breaking change in ASP.NET 4 see : this link and this link

Here is how i got it to working :

        if (HttpContext.Current != null)
            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(e));
        else 
            ErrorSignal.FromCurrentContext().Raise(e);

Somehow "Elmah.ErrorLog.GetDefault(HttpContext.Current).Log()" method works.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top