3

I have a web site with a lot of JavaScript that regularly seems to throw exceptions in production. I want to use something like NLog or Log4Net to log these exceptions, but the logger needs to run on the client as part of the JavaScript program and send log items to the server so they can be stored server side.

I would also like to log additional information (debug info, performance numbers). Would be great if I could easily switch loggers on or off.

Is there such a thing as an NLog for JavaScript that does all this? Any recommendations?

Rob Gutie
  • 31
  • 3

1 Answers1

2

You can set up an error handler and send your log data via AJAX (XMLHttpRequest). Here is an example from this site:

<script type="text/javascript" src="ajaxrequest.js"></script>
<script type="text/javascript">
    window.onerror = function(msg, url, line) {
        if(encodeURIComponent) {
            var req = new AjaxRequest();
            var params = "msg=" + encodeURIComponent(msg) + '&url=' + encodeURIComponent(url) + "&line=" + line;
            req.setMethod("POST");
            return req.loadXMLDoc("/logerror.php", params);
        }
        return false;
    }
</script>
kraxor
  • 649
  • 8
  • 16