4

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Those are the exact same links I posted and have already looked at :). Thanks for the work around though!
@Jake Oh! I didn't realize the links are the same. I was looking for a solution to the problem, i visited so many pages. I must have gotten lost somewhere :$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.