Documentation Menu

JavaScript Logging for ASP.NET Framework

1. Install adapter for your logging package

JSNLog needs an adapter to work with your specific logging package (Serilog, NLog, Log4Net, etc.)

Install one of the packages below to add the correct adapter to your web application. This also installs JSNLog itself if you haven't already done so.

These packages contain dependencies for both ASP.NET Framework and ASP.NET Core. However, versions below 2.8.60318.667 of the NuGet Package Manager installed in Visual Studio do not support this.

To check your Nuget version and upgrade if needed:

  1. In Visual Studio, open Tools | Extensions and Updates...;
  2. Click Updates (in left hand menu);
  3. If an update of NuGet Package Manager is available, click Update.
If you use Install this package
Log4Net Install-Package JSNLog.Log4Net
NLog Install-Package JSNLog.NLog
Elmah Install-Package JSNLog.Elmah
Serilog Install-Package JSNLog.Serilog
Common.Logging Install-Package JSNLog.CommonLogging

If your logging package is not listed here, install the version for Common.Logging and then install a Common.Logging adapter for your logging package.

2. Load jsnlog.js on your pages

Include a script tag in your web pages to load the jsnlog.js client side library from a free CDN. For example (more options):

@*Add to _Layout.cshtml*@

<script src="https://cdnjs.cloudflare.com/ajax/libs/jsnlog/2.28.0/jsnlog.min.js"></script>

Your _Layout.cshtml file would be a great place for this.

3. Serilog only

This package assumes that you have created a global logger. For example:

var log = new LoggerConfiguration()
    .WriteTo.File("log.txt")
    .CreateLogger();
// Set global logger, so JSNLog can access it. Log.Logger = log;

A good place for this would be the Application_Start handler in your Global.asax.cs.

4. OWIN only

A working demo is in project JSNLogDemo_Log4Net_OWIN

  1. Add JSNLog to your OWIN pipeline in your Startup class:

    using JSNLog;
    public class Startup { public void Configuration(IAppBuilder app) { ... // Short circuits log requests from the jsnlog.js client library and // passes the log data on to the server side logging package. app.UseJSNLog(); ... } }
  2. Update your web.config - Remove the definitions that make JSNLog work as an HTTP Handler, and add a definition that sends all log requests to the OWIN handler:

    <configuration>
        <system.web>
        <httpHandlers>
            ...
            <!-- Remove this line -->
                <add verb="*" path="*.logger" type="JSNLog.LoggerHandler, JSNLog" />
        </httpHandlers>
        </system.web>
        <system.webServer>
        <handlers>
            ...
            <!-- Remove these lines -->
                <add name="LoggerHandler" verb="*" path="*.logger" type="JSNLog.LoggerHandler, JSNLog" resourceType="Unspecified" preCondition="integratedMode" />
                <add name="LoggerHandler-Classic" path="*.logger" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode" />
            <!-- 
            Add this line. 
            If you configure JSNLog to send log request to a url that 
            does NOT end in .logger (how), change the path attribute below.
            -->
                <add name="Owin" verb="" path="*.logger" 
                 type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"/>
        </handlers>
        </system.webServer>
    </configuration>

  3. Add package - If you haven't already done so, add the Microsoft.Owin.Host.SystemWeb package:
    Install-Package Microsoft.Owin.Host.SystemWeb

5. Prevent script error obfuscation

If you load your own script files or external script files from different domains, consider adding crossorigin="anonymous" to prevent the browser from obfuscating script errors (details). For example:

<script crossorigin="anonymous" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

6. Start logging >>