How can I log client side javascript errors to the server? I'm using jQuery and MVC.
+2
A:
Since they're client-side errors, you'll have to send them to the server using XMLHttpRequest. You could use try...catch
statements or window.onerror
:
window.onerror(msg, url, line)
{
var message = "Error in "+url+" on line "+line+": "+msg;
$.post("logerror.aspx", { "msg" : message });
}
Be aware that line
and url
can be very inaccurate in IE versions prior to 8.
Andy E
2010-05-13 09:31:36
+1
A:
You could use my log4javascript, which has an appender that uses XMLHttpRequest
to log to the server:
var log = log4javascript.getLogger("serverLog");
var ajaxAppender = new log4javascript.AjaxAppender("clientlogger.jsp");
log.addAppender(ajaxAppender);
try {
nonExistentFunction();
} catch(ex) {
log.error("Something's gone wrong!", ex);
}
Tim Down
2010-05-13 09:54:58
Thanks for this suggestion. Do you know if it is possible to link this to log4net ?
Recursieve
2010-05-13 11:44:51
It's easy enough to do it manually. The default way the `AjaxAppender` sends logging data is as an HTTP post with parameters representing aspects of the logging event, such as the message, timestamp, severity etc. On the server, you'd have a page or HTTP handler to receive the post, and make a log4net call based on the post parameters.
Tim Down
2010-05-14 09:10:21