异常日志记录HttpModule不会从ASP .NET Page Method中捕获错误

cbp*_*cbp 5 asp.net webforms

我们有一个HttpModule,用于捕获异常并将它们记录到db.它看起来像这样:

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)
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这通常是有效的,但我注意到,当Page Methods中出现错误时,OnError方法永远不会触发(即使用WebMethod属性标记的代码隐藏文件中的方法).

怎么会?

除了重新实现Page方法本身内部的异常日志记录之外,我能做些什么吗?

Jas*_*ans 2

我在这里找到了一个适合我的解决方案:

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

这是我编写的处理程序的要点:

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...
    }        
}
Run Code Online (Sandbox Code Playgroud)