0

I need a way to handle and log all exceptions occuring in web api in one place. I override the methods of both ExceptionLogger and ExceptionHandler, but the code breaks and never triggers the handling mechanisms.

enter image description here

If I changed my controller to have to same API Methods, which cause another error:

[RoutePrefix("api/Ads")]
public class AdsController : ApiController
{
    public IHttpActionResult Get()
    {
        return Ok(Ad.GetAds());
    }
    public IHttpActionResult Get(object name)
    { 
        return Ok();
    }
}

The handler is going to catch the error:

enter image description here

In this case the error is :

  Multiple actions were found that match the request: 

Are the ExceptionLogger and ExceptionHandler catching only errors caused by web api at higher level? and what should i do to handle all errors, are Exception Filters the solution which have to be added also?

2 Answers 2

2

Add an Exception filter:

 public class MyExceptionFilter:ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        var t = HandleException(context);

        t.Wait();
    }

    public override async Task OnExceptionAsync(HttpActionExecutedContext context, CancellationToken cancellationToken)
    {
       await HandleException(context);
    }

    private Task HandleException(HttpActionExecutedContext context)
    {
        context.Response = context.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, context.Exception.Message);

        return Task.CompletedTask;
    }
}

Then in your WebApiConfig.Register method add this line:

config.Filters.Add(new MyExceptionFilter());
Sign up to request clarification or add additional context in comments.

6 Comments

I already added exception filters, but its not working also. I have also tried create my own exceptions and throwing them, but it didn't trigger any of the exception filter or exceptionLogger. I don't know may be I am missing something.
I could be wrong, but i don't think your Multiple actions were found that match the request: error will be handled by any in-code handling....i could be really wrong on that. I wouldn't expect it to anyway.
Yes, Because its already happening before executing an action. But, the question why any of the mechanisms will not handle an error thrown inside the action itself like "throw new Exception()".
If you have a ExceptionFilter registered at the app level (in the Register method), then what I posted should work. I would get rid of the GlobalExceptionHandler. Maybe post your Regsiter and StartUp methods for review.
If I should use ExceptionFilter this means i should discard the GlobalExceptionLogger that extends ExceptionLogger? so what is benefit of using a Filter in this case, taking into consideration that it catches only actions' exceptions while the other catches all exceptions. Can you please illustrate?
|
0

The ExceptionLogger and Filter was not catching any errors since the project was in debug mode, In debug mode it throws the exception and stop there, while in release mode the exception is caught by logger or filters.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.