Documentation Menu

JavaScript Logging for ASP.NET Core

Shows how to implement JavaScript logging in your ASP.NET Core web site.

1. Install server side component

If you haven't already done so, install the JSNLog NuGet package in your web application:

Install-Package JSNLog

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. Configure JSNLog middleware in Startup class

In the Configure method in your Startup class, add the JSNLog middleware before the UseStaticFiles middleware.

This short circuits logging requests from the jsnlog.js client side library and hands the log data off to your server side loggers.

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();

    ...
}

4. Start logging >>