Shows how to insert loggers in your JavaScript with
JSNLog, a JavaScript logging library that is well integrated with .Net.
It lets you configure loggers in your web.config.
And it lets you receive log messages from the client and store them on the server, without any coding.
Contents of this series
Inserting loggers in your JavaScript
Use the following methods to insert logging in your JavaScript.
getLogger
Gets a reference to a logger. If the logger does not yet exist, it will be created.
jsnlog.getLogger(loggerName);
Parameters
- loggerName
-
Type: string
The name of the logger.
Return Value
Type: logger
A reference to the logger with the given name.
Example
var logger = jsnlog.getLogger("yForms.Validators.IsNumeric.exceptionHandler");
Recommended naming
Although you can use any naming scheme for your loggers, it tends to make sense to use this structure:
namespace.objecttype.methodname.location within method
This makes it immediately clear where in your code the logger is located. For example:
var yForms = yForms || {};
yForms.Validators = function() {};
yForms.Validators.prototype.IsNumeric = function(inputValue) {
try {
...
} catch(err) {
jsnlog.getLogger("yForms.Validators.IsNumeric.exceptionHandler").fatal(err.message);
}
}
Obviously, if you do not have namespaces or objects, you could have logger names without those elements.
Appender and level inheritance
So that you do not have to configure an appender and level for every single logger,
a logger inherits the level and appenders from its parent. This uses the same scheme as
Log4Net.
JSNLog works out the parent of a logger by taking away one level of its name. This means that:
-
The parent of yForms.Validators.IsNumeric.exceptionHandler is yForms.Validators.IsNumeric
-
The parent of yForms.Validators.IsNumeric is yForms.Validators
-
The parent of yForms.Validators is yForms
The parent of yForms is a special logger, called the root logger. However, apart from the fact that this root logger
has no parent and no name, it
can still be used and configured as a normal logger.
getRootLogger
Returns a reference to the root logger.
jsnlog.getRootLogger();
Parameters
None
Return Value
Type: logger
A reference to the root logger.
Example
var rootLogger = jsnlog.getRootLogger();
Remarks
Normally, it makes sense to only use named loggers, because:
-
this allows you to configure each logger individually; and
-
helps
you identify the exact location in your code where a log message was generated.
However, if you decide to use only one logger, you could use the root logger.
trace
Gets a logger to send a message with severity TRACE.
logger.trace(message);
Parameters
- message
-
Type: object
Message to be sent. This can be any object, so doesn't have to be a string.
Return Value
None
Example
var logger = jsnlog.getLogger("yForms.Validators.IsNumeric.exceptionHandler");
logger.trace("Something interesting happened");
Or shorter:
jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").trace("Something interesting happened");
You can also send an object. JSNLog will produce a message showing the values of its fields:
var myValidator = new Validator();
jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").trace(myValidator);
debug
Similar to method trace, but sends a message of severity DEBUG.
jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").debug("Something interesting happened");
info
Similar to method trace, but sends a message of severity INFO.
jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").info("Something interesting happened");
warn
Similar to method trace, but sends a message of severity WARN.
jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").warn("Something interesting happened");
error
Similar to method trace, but sends a message of severity ERROR.
jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").error("Something interesting happened");
fatal
Similar to method trace, but sends a message of severity FATAL.
jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").fatal("Something interesting happened");
time
Starts a timer.
When the timer is ended with a call to
timeEnd
using the same timer name,
the amount of time that has elapsed in milliseconds since the timer was started is logged.
logger.time(timerName, messageLevel)
Parameters
- timerName
-
Type: string
Name assigned to the newly created timer.
- messageLevel (optional)
-
Type: level
One of:
jsnlog.Level.TRACE
jsnlog.Level.DEBUG
jsnlog.Level.INFO (default)
jsnlog.Level.WARN
jsnlog.Level.ERROR
jsnlog.Level.FATAL
The message sent by timeEnd will have this level.
If you do not supply a level, jsnlog.Level.INFO is used.
Return Value
None
Example
The following code uses a timer to measure how long a lengthy operation takes in milli seconds.
After the operation has finished, it sends a message with severity DEBUG with the measured time.
Keep in mind that in this case, the message will only be logged if the logger has level DEBUG or higher.
jsnlog.getLogger("yForms.Validators.IsNumeric.timer1").time("timer1", jsnlog.Level.DEBUG);
... lengthy operation ...
jsnlog.getLogger('yForms.Validators.IsNumeric.timer1').timeEnd('timer1');
timeEnd
Ends a timer that was started by time and sends a message with the elapsed time.
logger.timeEnd(timerName)
Parameters
- timerName
-
Type: string
Name of the timer as assigned by
time.
Return Value
None
Example
See the example provided for
time.
assertEqual
Compares two values using !=.
If they are equal, this does nothing. Otherwise,
it sends a message with severity ERROR.
logger.assertEqual(v1, v2)
Parameters
- v1, v2
-
Type: any
If these parameters are not equal, an ERROR log message is generated.
Return Value
None
Example
var expectedValue = ....;
var actualValue = ....;
jsnlog.getLogger('yForms.Validators.IsNumeric.AssertPositive').assertEqual(expectedValue, actualValue);
Next part
In this part 2 about JSNLog, you saw in detail all JavaScript functions provided by JSNLog.
The
next part,
will show all elements and attributes you can use in your web.config to configure your JavaScript loggers.
If you like this article, please vote for it.
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's blog is mattperdeck.com
Matt currently lives in Sydney, Australia, where he works at Readify, a high profile software consultancy.