Setting up Elmah.MVC

I am trying to get up and work with Elmah for an MVAP 5 WebAPI application. All the documents that I find begin and end with "installing it through nuget", and just suppose that it will cover it, but I obviously missed something. I installed Elmah.MVC 2.1.1 via nuget and I can view the error log in / elmah. I can request an invalid route and see that the received 404 error is logged by Elmah, even with customErrors enabled. However, if I create an unhandled error in the controller or while creating the controller instance, nothing is logged in Elmah. For instance:

[HttpGet, Route("api/health")]
[ApiExplorerSettings(IgnoreApi = true)]
public HttpResponseMessage GetHealth()
{
    throw new Exception("Whoops.");
}

I can call it all day and nothing appears in Elma magazine. Adding the standard HandleError attribute does not change anything, and the few docs here seem pretty obvious that I don't need to create a custom HandleError attribute.

I use Ninject for DI, and similarly, if I break the dependency binding, I will get a good error, for example:

{
    Message: "An error has occurred."
    ExceptionMessage: "An error occurred when trying to create a controller of type 'HealthController'. Make sure that the controller has a parameterless public constructor."
    ...
}

But in this case, nothing turns him into Elma’s magazine.

I assume that in both cases this is because MVC handles these errors for me (formatting exceptions as JSON, etc.) and not passing them to Elmah. How to configure everything so that any exception not handled in the controller is logged by Elmah?

+4
source share
1

. ExceptionFilter, , ExceptionFilters , , . -, ExceptionLogger Elmah:

public class ElmahExceptionLogger : ExceptionLogger
{
    public override void Log(ExceptionLoggerContext context)
    {
        Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(context.Exception));
    }
}

, WebApiConfig.cs:

public static void Register(HttpConfiguration config)
{
    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Services.Add(typeof(IExceptionLogger), new ElmahExceptionLogger());
}

MVC/WebAPI ( 5.1.1).

+1

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


All Articles