Configuring JSNLog using server code
JSNLog has 2 componets:
- A server side component that receives log messages from the client and hands them to a server side logging package. This is obviously configured on the server.
- A client side component (jsnlog.js) where the client side loggers live. JSNLog lets you configure this on either the server (described on this page) or the client (details).
This page first shows how to configure JSNLog using code or in your web.config.
It then shows how to pass your configuration settings from the server to the client.
Configure JSNLog using code
You can configure JSNLog by calling the SetJsnlogConfiguration method when your site starts.
This method takes a JsnlogConfiguration object. This object has all the JSNLog configuration settings, including those for loggers, appenders, etc.
More details and examples about calling SetJsnlogConfiguration are
here
Configure JSNLog in your web.config
First make sure there is an jsnlog section in your <configSections> section:
<configuration>
<configSections>
<section name="jsnlog" type="JSNLog.ConfigurationSectionHandler, JSNLog"
requirePermission="false" />
...
</configSections>
...
</configuration>
You can now configure JSNLog by adding a <jsnlog> section inside the <configuration> section, and by optionally adding <logger>, <ajaxAppender> and <consoleAppender> subsections.
For example:
<configuration>
<configSections>
<section name="jsnlog" type="JSNLog.ConfigurationSectionHandler, JSNLog"
requirePermission="false" />
...
</configSections>
...
<jsnlog>
<logger level="3000" />
</jsnlog>
</configuration>
Pass your configuration settings from the server to the client
When you configure client side loggers, appenders, etc. on the server, that configuration information has to somehow travel to the client, so jsnlog.js can create the loggers, etc. you configured.
Call Configure method
To make that happen, call JSNLog's Configure method in your pages, before the first JavaScript script tags.
-
For Razor (MVC3+), use:
@*Add to your pages before the script tags*@ @Html.Raw(JSNLog.JavascriptLogging.Configure())
Your _Layout.cshtml or _Layout.vbhtml would probably be a good place for this.
-
For WebForms, use:
@*Add to your pages before the script tags*@ <%= JSNLog.JavascriptLogging.Configure() %>
Your master page would probably be a good place for this.
This translates the server side configuration into
calls to
configuration methods in jsnlog.js
This automatically generates a script tag
The Configure method automatically generates a script tag that loads the jsnlog.js JavaScript library.
To stop this, edit your web.config and make sure the <jsnlog> tag doesn't have a productionLibraryPath attribute:
<jsnlogproductionLibraryPath="~/Scripts/jsnlog.min.js"> </jsnlog>