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

Install JSNLog NuGet package for ASP.NET Core:

Install-Package JSNLog.AspNetCore

2. Load jsnlog.js on your pages

Include a script tag in your ASP.NET Core web site, such as this (more options):

@*Add to _Layout.cshtml*@

<script src=""></script>

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

3. Configure JSNLog middleware in Startup.cs

In your Startup.cs file, add the JSNLog middleware to your ASP.NET Core application 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);


4. Optional: Configure client side loggers with server side code

You can configure your client side loggers in your server side code, by initializing a JsnlogConfiguration object (how) and passing it to the UseJSNLog method as shown in the listing above. This way you can use the ASP.NET Core Configuration features, which are pretty cool.

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 />

Note that this is purely optional. JSNLog works fine out of the box without configuration. You can also configure your loggers in your JavaScript.

5. Start logging >>