1. 程式人生 > >前端異常日誌記錄

前端異常日誌記錄

string navig ner mage home window uri cati agen

面對復雜的前端環境,進行前端異常日誌記錄很有必要,基於 window.onerror 實現:

// 基於window.onerror 收集前端錯誤信息
window.onerror = function (message, url, line) {
    if (!url) return;

    var msg = {
        ua: window.navigator.userAgent,
        message: message,
        url: url,
        line: line,
        page: window.location.href
    };

    
new Image().src = ‘/Home/UiErrorLog?error=‘ + encodeURIComponent(JSON.stringify(msg)) + ‘&t=‘ + Math.random(); };

前端異常日誌記錄