5

We have an HttpModule that is designed to catch exceptions and log them to the db. It looks something like this:

public class ExceptionLoggingModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.Error += OnError;
    }

    private static void OnError(object sender, EventArgs e)
    {
        try
        {
            var context = (HttpApplication) sender;
            var exception = context.Server.GetLastError();

            if (exception != null)
            {
                // Log exception
            }
        }
        catch(Exception)
        {
        }
    }
}

This works in general, but I've just noticed that the OnError method never fires when an error occurs within Page Methods (i.e. methods in a code behind file marked with the WebMethod attribute).

How come?

Is there something I can do about this, other than reimplementing the exception logging inside the Page Method itself?

1
  • I'm experiencing the same problem today. Commented Nov 12, 2012 at 17:01

1 Answer 1

2

I found a solution that worked for me here:

http://blogs.microsoft.co.il/blogs/oshvartz/archive/2008/05/17/asp-net-error-handling-using-httpmodule-full-and-partial-post-back-ajax-updatepanel.aspx

Here's the key points of the handler I've written:

public class PathfinderModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PostMapRequestHandler += this.OnPostMapRequestHandler;
        context.Error += OnError;
    }

    private void OnPostMapRequestHandler(object sender, EventArgs e)
    {
        Page aux = HttpContext.Current.Handler as Page;

        if (aux != null)
        {
            aux.Error += this.OnPageError;
        }
    }

    private static void OnError(object sender, EventArgs e)
    {
        // Blah..
    }

    private void OnPageError(object sender, EventArgs e)
    {
        // Blah...
    }        
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hm this doesn't work from me - if I throw an exception from inside a PageMethod, the event handler doesn't fire.
Part1: Check the basics - web.config contains the right settings and those settings are formatted/spelled correctly (I've been caught out this a few times). Restart IIS/app pool just to be sure. Place a breakpoint inside the Init() method and attach Visual Studio to the w3wp.exe process which is hosting your site. You want to know that the code is executing
Part 2: If the attch method doesn't work, then put some Trace.WriteLine() calls in the code so that you can see if that code is being called. Use a tool like DebugOutput from Sysinternals to pick up the WriteLine calls. If your code is not executing at all, then I'm not sure what it could be, sorry.
Hi thanks, with all the necessary breakpoints I can see it is executing when exceptions are raised during the normal Page lifecycle, but not when raised by PageMethods.
OK, it might be worth you opening a new question, with a reference to this question, and post the code you are using. Sounds like you may have a different issue then what I suffered. Will make it easier to help you if you discuss it in its own question.

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.