在服务器上记录客户端 JavaScript 错误

我正在运行一个 ASP.NET 站点,在那里我发现了一些手动测试的 JavaScript 错误。

有没有可能在客户端捕获所有的 JavaScript 错误并将它们记录到服务器上,比如在 EventLog 中(通过 Webservice 或类似的东西) ?

55386 次浏览

You could potentially make an Ajax call to the server from a try/catch, but that's probably about the best you can do.

May I suggest JavaScript unit testing instead? Possibly with JSUnit?

You could try setting up your own handler for the onerror event and use XMLHttpRequest to tell the server what went wrong, however since it's not part of any specification, support is somewhat flaky.

Here's an example from Using XMLHttpRequest to log JavaScript errors:

window.onerror = function(msg, url, line)
{
var req = new XMLHttpRequest();
var params = "msg=" + encodeURIComponent(msg) + '&url=' + encodeURIComponent(url) + "&line=" + line;
req.open("POST", "/scripts/logerror.php");
req.send(params);
};

Also I recomend using TraceTool utility, it comes with JavaScript support and is very handy for JS monitoring.

If you're wanting to log the client-side errors back to the server you're going to have to do some kind of server processing. Best bet would be to have a web service which you can access via JavaScript (AJAX) and you pass your error log info to it.

Doesn't 100% solve the problem cuz if the problem is with the web server hosting the web service you're in trouble, you're other option would be to send the info via a standard page via a query string, One method of doing that is via dynamically generating Image tags (which are then removed) as the browser will try and load the source of an image. It gets around cross-domain JavaScript calls nicely though. Keep in mind that you're in trouble if someone has images turned off ;)

I have just implemented server side error logging on javascript errors on a project at work. There is a mixture of legacy code and new code using jQuery.

I use a combination of window.onerror and wrapping the jQuery event handlers and onready function with an error handling function (see: JavaScript Error Tracking: Why window.onerror Is Not Enough).

  • window.onerror: catches all errrors in IE (and most errors in Firefox), but does nothing in Safari and Opera.
  • jQuery event handlers: catches jQuery event errors in all browsers.
  • jQuery ready function: catches initialisation errors in all browsers.

Once I have caught the error, I add some extra properties to it (url, browser, etc) and then post it back to the server using an ajax call.

On the server I have a small page which just takes the posted arguments and outputs them to our normal server logging framework.

I would like to open source the code for this (as a jQuery plugin). If anyone is interested let me know, it would help to convince the bosses!

Short answer: Yes, it is possible.

Longer answer: People have already written about how you can (at least partially) solve this issue by writing your own code. However, do note that there are services out there that seems to have made sure the JS code needed works in many browsers. I've found the following:

I can't speak for any of these services as I haven't tried them yet.

I've been using Appfail recently, which captures both asp.net and JavaScript errors

If you use Google Analytics, you can log javascript errors into Google Analytics Events.

See this app: http://siteapps.com/app/log_javascript_errors_with_ga-181

Hope it helps.