前端异常日志记录

面对复杂的前端环境,进行前端异常日志记录很有必要,基于 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();
};

 

posted @ 2017-12-27 10:13  _York  阅读(401)  评论(0编辑  收藏  举报