Click here to Skip to main content
15,664,808 members
Articles / Programming Languages / Javascript
Posted 14 Sep 2013


52 bookmarked

Include JavaScript exceptions in your server side logs with JSNLog

Rate me:
Please Sign up or sign in to vote.
4.94/5 (21 votes)
29 Apr 2014CPOL2 min read
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.


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.

Image 1

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.

catch (Exception e)
    ILog log = LogManager.GetLogger("serverlogger");
Image 2

But what about exceptions in your JavaScript code?

Image 3

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.

    // 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) {
        "msg": "something went wrong!", 
        "errorMsg": errorMsg, "url": url, 
        "line number": lineNumber, "column": column
    }, errorObj);
    return false;

More about JavaScript exception logging

Image 4


  • 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.


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

Written By
Australia Australia
Twitter: @MattPerdeck
Current project: JSNLog JavaScript Logging Package

Matt has over 9 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 is the author of the book ASP.NET Performance Secrets ( 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. He recently worked at Readify and the global professional services company PwC. He now works at SP Health, a global provider of weight loss web sites such at CSIRO's and

Comments and Discussions

QuestionJSNlog with Nlog config not working?? Pin
Member 1358974621-Dec-17 23:05
Member 1358974621-Dec-17 23:05 
QuestionExcelent Pin
Aladár Horváth6-Aug-15 21:54
professionalAladár Horváth6-Aug-15 21:54 
QuestionCan System.Diagnostics be used for logging on the server side? Pin
klachko22-Sep-14 3:34
klachko22-Sep-14 3:34 
GeneralMy vote of 5 Pin
Volynsky Alex29-Apr-14 23:00
professionalVolynsky Alex29-Apr-14 23:00 
GeneralMy vote of 5 Pin
JOE Heart Under Blade15-Nov-13 14:59
JOE Heart Under Blade15-Nov-13 14:59 
GeneralMy vote of 5 Pin
Prasad Khandekar10-Nov-13 23:51
professionalPrasad Khandekar10-Nov-13 23:51 
QuestionImpact on bandwidth Pin
Md. Shafiuzzaman10-Nov-13 22:21
professionalMd. Shafiuzzaman10-Nov-13 22:21 
AnswerRe: Impact on bandwidth Pin
Matt Perdeck10-Nov-13 23:22
Matt Perdeck10-Nov-13 23:22 
GeneralRe: Impact on bandwidth Pin
Md. Shafiuzzaman11-Nov-13 0:56
professionalMd. Shafiuzzaman11-Nov-13 0:56 
QuestionUsing JSNLog with Elmah Pin
Nikfazan14-Sep-13 22:41
professionalNikfazan14-Sep-13 22:41 
AnswerRe: Using JSNLog with Elmah Pin
Matt Perdeck15-Sep-13 2:24
Matt Perdeck15-Sep-13 2:24 
GeneralRe: Using JSNLog with Elmah Pin
kiquenet.com5-May-14 0:30
professionalkiquenet.com5-May-14 0:30 
QuestionDebug and Release mode? Pin
Nitij14-Sep-13 22:29
professionalNitij14-Sep-13 22:29 
AnswerRe: Debug and Release mode? Pin
Matt Perdeck15-Sep-13 2:41
Matt Perdeck15-Sep-13 2:41 

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

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