Documentation Menu

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 in your Startup class Configure method. 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 in your Startup class Configure method

All server side configuration, including loggers, appenders, etc., can be defined using the JsnlogConfiguration class.

To configure JSNLog in your server side code:

  1. Create an JsnlogConfiguration object with the configuration properties you want;
  2. Pass it to the UseJSNLog method that configures JSNLog in your middleware pipeline.

The result will look like this:

using JSNLog;
                    
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...
    
    // Configure JSNLog
    var jsnlogConfiguration = new JsnlogConfiguration();
    app.UseJSNLog(new LoggingAdapter(loggerFactory), jsnlogConfiguration);
    
    app.UseStaticFiles();

    ...
}

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.

To make this work, call JSNLog's jl-javascript-logger-definitions tag helper in your pages, before the first JavaScript script tags.

First import the tag helper via your _ViewImports.cshtml file:

@*Add to _ViewImports.cshtml*@

@addTagHelper "*, jsnlog"

(The _ViewImports.cshtml file lives in your Views directory, next to your _ViewStart.cshtml file)

Then insert the tag helper before the first JavaScript script tag. Your _Layout.cshtml file would be a great place for this:

@*Add to _Layout.cshtml*@

<jl-javascript-logger-definitions />

This translates the server side configuration into calls to configuration methods in jsnlog.js. It then injects this JavaScript code as a script block into your page.

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:

<jsnlog productionLibraryPath="~/Scripts/jsnlog.min.js">
</jsnlog>