DottorCry DottorCry
DottorCry DottorCry

Reputation: 29

ASP.NET Problem with IHttpModule catch error

I made an API project with basic authentication.

To manage basic authentication I created a module that extends the IHttpModule class. The module is composed of an Init as follows

    public void Init(HttpApplication context)
    {
        // Register event handlers
        context.BeginRequest += new EventHandler(begin_request);
        context.AuthenticateRequest += OnApplicationAuthenticateRequest;
        context.Error += new EventHandler(OnError);
        context.EndRequest += OnApplicationEndRequest;

    }

I'm trying to catch all errors that occur (especially 500 errors) through OnError:

private void OnError (object obj, EventArgs args)
     {
         // At this point we have information about the error

         HttpContext ctx = HttpContext.Current;
         HttpResponse response = ctx.Response;
         HttpRequest request = ctx.Request;

         Exception exception = ctx.Server.GetLastError ();

         Logger.WriteError (exception.ToString ());

       
         string errorInfo = "<p /> URL:" + ctx.Request.Url.ToString ();
         errorInfo + = "<p /> Stacktrace: --- <br/>" +
            exception.InnerException.StackTrace.ToString ();
         errorInfo + = "<p /> Error Message: <br/>" +
            exception.InnerException.Message;

         Logger.WriteError (errorInfo);

         ctx.Server.ClearError ();
     }

this method is never called even if there are errors.

What's the problem?

Thanks

Upvotes: 1

Views: 58

Answers (0)

Related Questions