Documentation Menu

SetJsnlogConfiguration Method

Lets you configure JSNLog in code.

Definition

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

Parameters

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

Remarks

jsnlogConfiguration

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).

loggingAdapter

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, or ASP.NET 5's logging framework.

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.

ILoggingAdapter

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"
                }
            }
        });
    }
}