Code Monkey home page Code Monkey logo

jsnlog.angularjs's Introduction

Logging and JavaScript error handling for AngularJS

About logToServer.js

logToServer.js is an AngularJs module that makes it easy to log exceptions, AJAX timeouts and other events from your client side code to your server side log.

It is essentially an AngularJs layer on top of JSNLog, a JavaScript logging library that lets you log events in JavaScript code and automatically store them in your server side log. Supports Elmah, NLog, Log4Net, Serilog and any other package with a Common.Logging adapter.

The logToServer module:

  • Logs uncaught JavaScript exceptions to the server side log - Replaces the AngularJS $exceptionHandler with a version that logs JavaScript exceptions to your server side log (NLog, Log4Net, etc.)
  • Logs AJAX issues - Provides Interceptors to log AJAX errors and timeouts to the server, and to log warning messages when AJAX responses are slow.
  • Logs debug messages etc. to the server from JavaScript code - Replaces the standard AngularJS $log with a version that logs to the server, and optionally to the console using a consoleAppender.

Working demo code

In the Demo directory you'll find the working source code of a very simple demo site that puts all this in action.

  • Download the zip file of this project. Unzip in some directory.
  • The solution file is in Demo/JSNLog.AngularJS.Demo. Open that in Visual Studio. If you don't have Visual Studio, get Visual Studio Express for free.
  • Rebuild the solution. This will import the Nuget packages.
  • Hit F5 to run the demo site. You'll see some buttons to generate events. Hit a few.
  • This demo uses Elmah as the server side logging package. Open http://localhost:*portnumber*/elmah.axd to see the log messages written to the log. Besides Elmah, JSNLog supports NLog, Log4Net, Serilog and any other logging package for which there is a Common.Logging adapter.

Install logToServer.js

Note that JSNLog does not include logToServer.js. The two packages have to be installed separately.

  1. Install the JSNLog JavaScript Logging Package.

  2. Install logToServer.js:

    • Using bower:
    bower install logToServer.js
    
    • Or copy directly from this repo to for example your Scripts directory (get the file here).
  3. Make sure that logToServer.js gets loaded into your web pages, for example with a script tag.

Log uncaught exceptions and other events to the server

Simply import module logToServer into your main module, like so:

var app = angular.module('mainmodule', ['logToServer']);

This logs uncaught exceptions to the server. If you do any debug logging, those messages will go to the server as well.

You can configure your client side loggers (setting severity levels, etc.) using a server side configuration file, or directly from your JavaScript code.

Log AJAX issues

First add the interceptors to the interceptors array for your application:

app.config(['$httpProvider', function($httpProvider) {
	$httpProvider.interceptors.push('logToServerInterceptor');
}]);

Now when you call $http to send an AJAX message, set the timeout and the delay after which a warning log message is sent in milliseconds in the config object, using timeout and warningAfter:

$http({ method: '...', url: '...', data: ..., timeout: 5000, warningAfter: 2000 })
.success(function (data, status, headers, config) {
	...
})
.error(function (data, status, headers, config) {
	...
});

Or when using the new type of promises:

$http({ method: '...', url: '...', data: ..., timeout: 5000, warningAfter: 2000 })
.then(function(response) {
	...
})
.catch(function (rejection) {
	...
})
.finally(function () {
	...
});

Documentation

More about using JSNLog to handle errors in AngularJS apps

jsnlog.angularjs's People

Contributors

mperdeck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jsnlog.angularjs's Issues

more elegant way to implement this

Hi

Here is more convenient to implement this

http://vinnylinck.tumblr.com/post/58833687265/angularjs-how-to-override-log-implementation

    angular.module('bk.jsnlog', [])
        .config(['$provide', function ($provide) {
            $provide.decorator('$log', ['$delegate', '$logShadowLogger', function ($delegate, $logShadowLogger) {
                return $logShadowLogger($delegate);
            }]);
        }])
        .factory('$logShadowLogger', function () {

            return function ($delegate) {

                return {
                    log: function () {
                        $delegate.log.apply($delegate, arguments);
                    },
                    info: function () {
                        $delegate.info.apply($delegate, arguments);
                    },
                    error: function () {
                        $delegate.error.apply($delegate, arguments);
                    },
                    warn: function () {
                        $delegate.warn.apply($delegate, arguments);
                    }
                };

            };
        });

Error: [$rootScope:inprog] $digest already in progress

Hi Matt,
Thank you very much for the wonderful libraries. There are really the best. With angularjs 1.5.3, the logging to server works, however, I receive error:
Error: [$rootScope:inprog] $digest already in progress
Can you please help me?
thx
--lola

Issue while running in VS2015

I am running the provided .NET code on VS2015 and receiving an error "cs0234: The type or namespace name 'JavascriptLogging' does not exist in the namespace 'JSNLog'"

Any thought from where I can get JSNlog file?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.