Documentation Menu

SetJsnlogConfiguration Method

Lets you configure JSNLog in code.


public static void SetJsnlogConfiguration(
    JsnlogConfiguration jsnlogConfiguration, ILoggingAdapter loggingAdapter = null)


jsnlogConfiguration Configuration object. See remarks.
loggingAdapter Optional. Adapter to the server side logging package. See remarks.


This method is only useful with .Net Framework

About server side configuration when using ASP.NET Core.


By default, JSNLog tries to get its configuration from your web.config file. If there is no such file or if it does not have an <jsnlog> element, it uses its default configuration (which works fine in most cases).

However, you may want to configure JSNLog using code instead. To do this, pass a JsnlogConfiguration object with your configuration settings to this method.

To switch JSNLog back to the default situation, pass null to the jsnlogConfiguration parameter.

To avoid confusion, JSNLog does not allow you to use both code based configuration and web.config based configuration. If you want to use code based configuration, remove the <jsnlog> element from your web.config (if you have one).


When JSNLog receives a log request from the client, it needs to pass on the log information to your server side logging package.

By default, it does this by calling a Common.Logging interface. The Common.Logging package in turn looks at your web.config to determine which server side logging package to talk to (such as Log4Net, NLog, etc.)

You can override this behaviour by setting your own adapter to the server side logging package. This allows you to use an alternative logging abstraction such as LibLog.

To make this happen, create a class implementing ILoggingAdapter. Then call SetJsnlogConfiguration with an object of that class to replace JSNLog's default adapter.

If you pass in null to the loggingAdapter parameter, the current adapter is not changed.


Implementing ILoggingAdapter should not be hard (example):

public interface ILoggingAdapter
    void Log(FinalLogData finalLogData);

Your new Log method takes a FinalLogData object and logs its content on the server, in whatever way you see fit.

Calling SetJsnlogConfiguration

You would call SetJsnlogConfiguration at the start of each request:

  • If you use OWIN, in the Configuration method in your Startup class.
  • Otherwise, in the Application_BeginRequest event handler in your Global.asax.cs file as shown below.
// Global.asax.cs
// Simple example configuration in code
using JSNLog;

public class MvcApplication : System.Web.HttpApplication
    protected void Application_BeginRequest()
        JavascriptLogging.SetJsnlogConfiguration(new JsnlogConfiguration
            serverSideMessageFormat = "%logger, %level, %message",
            loggers = new List<Logger>
                new Logger 
                    name = "jsLogger",
                    level = "FATAL"

Calling SetJsnlogConfiguration from a class library

If you call SetJsnlogConfiguration or any other JSNLog method from a class library, do not install the JSNLog package in your class library. Instead, install JSNLog.ClassLibrary:

Install-Package JSNLog.ClassLibrary

The problem with installing JSNLog is that this will install the JSNLog JavaScript libraries as well. It will also add a App_Start\JSNLogConfig.cs file and try to modify the project's web.config. Good for web site projects, but superfluous for class libraries.

Additionally, if you were to make your class library into a nuget package, then that package would have a dependency on JSNLog. Then when you install your class library into other class libraries, those other class libraries would receive the JavaScript libraries and App_Start\JSNLogConfig.cs file as well.

Installing JSNLog.ClassLibrary prevents all this. This package is the same as the JSNLog package, except that it does not add the JavaScript libraries and App_Start\JSNLogConfig.cs. It also does not try to modify the web.config file. So it is great for class libraries.