Configuring JSNLog using server code
JSNLog has 2 componets:
- A server side component that receives log messages from the client and hands them to a server side logging package. This is obviously configured on the server.
- A client side component (jsnlog.js) where the client side loggers live. JSNLog lets you configure this on either the server (described on this page) or the client (details).
This page first shows how to configure JSNLog in your Startup class Configure method.
It then shows how to pass your configuration settings from the server to the client.
Configure JSNLog in your Startup class Configure method
All server side configuration, including loggers, appenders, etc., can be defined using the JsnlogConfiguration class.
To configure JSNLog in your server side code:
- Create an JsnlogConfiguration object with the configuration properties you want;
- Pass it to the UseJSNLog method that configures JSNLog in your middleware pipeline.
The result will look like this:
public void Configure(..., ILoggerFactory loggerFactory)
{
...
app.UseJSNLog(loggerFactory, new JsnlogConfiguration()
{
...
});
...
Pass your configuration settings from the server to the client
When you configure client side loggers, appenders, etc. on the server, that configuration information has to somehow travel to the client, so jsnlog.js can create the loggers, etc. you configured.
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 />
This translates the server side configuration into
calls to
configuration methods in jsnlog.js