ASP.NET MVC ELMAH does not register HttpRequestValidationExceptions

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

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

It works correctly and sends me an email and logs 404 errors, but when I enter some html in the input <h1>Test</h1>to see how the site is processed, I get an HttpRequestValidationException, which is good in the sense that it will not let them throw it in and I have a page setup with an error that will be displayed when the site is published, but Elmah does not register information about this error.

I looked at this SO post and Elmah issue 217 says that it was 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));
    }
}

What I indicated 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 log Elmah email data about these kinds of errors?

thank.

+4
source share
1 answer

This problem seems to be related to breaking changes in ASP.NET 4 see this link and this link

Here's how I got it to work:

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

The Elmah.ErrorLog.GetDefault (HttpContext.Current) .Log () method somehow works.

+1
source

Source: https://habr.com/ru/post/1533817/


All Articles