Click here to Skip to main content
Click here to Skip to main content
Go to top

Include JavaScript exceptions in your server side logs with JSNLog

, 29 Apr 2014
Rate this:
Please Sign up or sign in to vote.
JSNLog lets you insert loggers in your client side JavaScript, configure them in your web.config, and store their messages in your server side logs.

Integrating JavaScript exception logging with your server side logs.

Introduction

The JSNLog JavaScript logging package is a JavaScript counterpart to Log4Net, NLog, Elmah, etc. It lets you insert loggers in your JavaScript code. It automatically sends the log messages to a server side component which then inserts them in your existing server side log. It supports Log4Net, NLog, Elmah and Common.Logging.

Because it has sensible default configuration settings, you can start logging right away after you have installed the JSNLog Nuget package.

It has many options, from stack traces for JavaScript exceptions to request ids to correlate messages and various filters to cut down the volume of log messages. You can also log to the browser console, so there is no more need to litter your code with console.log statements. This can all be configured in your web.config, or in your JavaScript.

Why and how

Your web site consists of C#/VB code running on the server, and JavaScript running on the browser.

When an exception happens in your C#/VB code, you log it, so you can fix the bug.

Using your server side logging package, such as Log4Net, NLog or Elmah, with loggers configured via web.config.

try
{
    ....
}
catch (Exception e)
{
    ILog log = LogManager.GetLogger("serverlogger");
    log.Fatal(e.ToString());
}

But what about exceptions in your JavaScript code?

try
{
    ....
}
catch(err)
{
    ????
}

Log the JavaScript exception on the client with JSNLog, which sends it to the server.

JSNLog will pass the JavaScript exception log message with its stack trace on to your server side logging package, so it winds up in your server side logs. You can log extra information, such as the values of variables.

try
{
    ....
}
catch(e)
{
    // Log the exception, complete with stack trace
    JL("jsLogger").fatalException({ "msg": "something went wrong!", "variable1": variable1, ... }, e);
}

Or use a window.onerror handler, to catch all uncaught exceptions:

window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {
    JL("onerrorLogger").fatalException({
        "msg": "something went wrong!", 
        "errorMsg": errorMsg, "url": url, 
        "line number": lineNumber, "column": column
    }, errorObj);
        
    return false;
}

More about JavaScript exception logging

Benefits

  • Extensive documentation. Easy to install.
  • No need to pay fees to a third party logging service.
  • JavaScript log messages go into the same logs as your server side log messages.
  • Or send JavaScript log messages to the browser console.
  • Uses a tiny JavaScript library that can be loaded as an AMD module or with a simple script tag, or as part of a bundle.
  • Request ids correlate JavaScript log messages and server side log messages generated by the same user session.
  • Configure JavaScript loggers in your web.config.
  • Many filtering options to prevent flooding your server with JavaScript log messages.
  • Option to send JavaScript log messages in batches of 2 or more for greater efficiency.
  • Logs JSON objects as well as strings.

Next step

If you like this article, please vote for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Matt Perdeck
Web Developer
Australia Australia
Twitter: @MattPerdeck
Blog: mattperdeck.com
Current project: JSNLog JavaScript Logging Package
 
Matt has over 6 years .NET and SQL Server development experience. Before getting into .Net, he worked on a number of systems, ranging from the largest ATM network in The Netherlands to embedded software in advanced Wide Area Networks and the largest ticketing web site in Australia. He has lived and worked in Australia, The Netherlands, Slovakia and Thailand.
 
He recently wrote a book, ASP.NET Performance Secrets (www.packtpub.com/asp-net-site-performance-secrets/book) in which he shows in clear and practical terms how to quickly find the biggest bottlenecks holding back the performance of your web site, and how to then remove those bottlenecks. The book deals with all environments affecting a web site - the web server, the database server and the browser.
 
Matt currently lives in Sydney, Australia. After 2 years at Readify, he now works at the global professional services company PwC. His current contract ends at 29 June 2014.
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalVolynsky Alex29-Apr-14 23:00 
GeneralMy vote of 5 PinmemberJOE MIS15-Nov-13 14:59 
GeneralMy vote of 5 PinprofessionalPrasad Khandekar10-Nov-13 23:51 
QuestionImpact on bandwidth PinprofessionalMd. Shafiuzzaman10-Nov-13 22:21 
AnswerRe: Impact on bandwidth PinmemberMatt Perdeck10-Nov-13 23:22 
GeneralRe: Impact on bandwidth PinprofessionalMd. Shafiuzzaman11-Nov-13 0:56 
QuestionUsing JSNLog with Elmah PinprofessionalNikfazan14-Sep-13 22:41 
AnswerRe: Using JSNLog with Elmah PinmemberMatt Perdeck15-Sep-13 2:24 
GeneralRe: Using JSNLog with Elmah Pinmemberkiquenet.com5-May-14 0:30 
QuestionDebug and Release mode? PinprofessionalNitij Kumar14-Sep-13 22:29 
AnswerRe: Debug and Release mode? PinmemberMatt Perdeck15-Sep-13 2:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 29 Apr 2014
Article Copyright 2013 by Matt Perdeck
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid