You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何捕获ASP.NET Ajax Control Toolkit未处理异常?

针对ASP.NET UpdatePanel AXD请求异常的处理方案

方案一:捕获错误发生时的请求详情

利用ASP.NET AJAX内置的Sys.WebForms.PageRequestManager事件,可直接获取错误关联的页面和请求信息:

// 确保在PageRequestManager初始化完成后注册事件
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(sender, args) {
    if (args.get_error()) {
        const error = args.get_error();
        // 匹配你提到的两类目标异常
        const isTargetError = 
            error.name === 'Sys.WebForms.PageRequestManagerTimeoutException' ||
            (error.name === 'Sys.WebForms.PageRequestManagerServerErrorException' && 
             (error.message.includes('404') || error.message.includes('503')));

        if (isTargetError) {
            // 收集请求上下文信息
            const errorContext = {
                pageUrl: window.location.href,
                errorType: error.name,
                errorMessage: error.message,
                // 从异常中提取状态码
                statusCode: error.message.match(/status code returned from the server was:\s*(\d+)/)?.[1] || 'N/A'
            };
            
            // 可手动上报到Elastic,或先打印调试
            console.log('UpdatePanel预期错误详情:', errorContext);
        }
    }
});

如果需要追踪具体的ScriptResource.axd请求URL,可通过拦截XMLHttpRequest实现:

// 拦截XMLHttpRequest,记录AXD请求
const originalXhrOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url) {
    if (url.includes('ScriptResource.axd')) {
        this._axdRequestInfo = {
            url: url,
            fromPage: window.location.href
        };
    }
    originalXhrOpen.apply(this, arguments);
};

// 结合PageRequestManager事件关联错误与请求
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(sender, args) {
    if (args.get_error()) {
        const xhr = args.get_response()?._xmlHttpRequest;
        if (xhr?._axdRequestInfo) {
            console.log('AXD请求URL:', xhr._axdRequestInfo.url, '发起页面:', xhr._axdRequestInfo.fromPage);
        }
    }
});

方案二:将预期异常标记为已处理,过滤Elastic捕获

既然这类UpdatePanel请求失败属于预期情况,直接阻止Elastic捕获即可,有两种实现方式:

方式1:通过PageRequestManager标记错误已处理

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(sender, args) {
    if (args.get_error()) {
        const error = args.get_error();
        const isExpectedError = 
            error.name === 'Sys.WebForms.PageRequestManagerTimeoutException' ||
            (error.name === 'Sys.WebForms.PageRequestManagerServerErrorException' && 
             (error.message.includes('404') || error.message.includes('503')));
        
        if (isExpectedError) {
            // 标记错误已处理,浏览器不会抛出未捕获异常,Elastic也就不会收录
            args.set_errorHandled(true);
        }
    }
});

方式2:通过全局错误钩子过滤

如果Elastic依赖全局window.onerror捕获错误,可直接在钩子中过滤:

window.onerror = function(message, source, lineno, colno, error) {
    if (error?.name === 'Sys.WebForms.PageRequestManagerTimeoutException' || 
        (error?.name === 'Sys.WebForms.PageRequestManagerServerErrorException' && 
         (error.message.includes('404') || error.message.includes('503')))) {
        // 返回true表示错误已处理,终止传播
        return true;
    }
    // 非目标错误交给Elastic处理
    return false;
};

注意事项

  • 所有脚本需确保在Sys.WebForms.PageRequestManager对象可用后执行,建议放在页面底部或ScriptManager的load事件中。
  • 若使用Elastic官方JS Agent,可直接配置ignoreErrors规则匹配异常信息,更简洁高效。

内容的提问来源于stack exchange,提问作者DaveyBoy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 08:25:20