Documentation Menu

Setting the url to send logs to

By default, the jsnlog.js library sends all log requests to the URL /jsnlog.logger. This page shows how to change this.

Change the URL

An AjaxAppender goes through these steps to work out what url to send log requests to:

  1. If has its own url, it will use that - set the url attribute of the AjaxAppender.

    Note that the default appender does not have its own url, so it will always default to step 2.

  2. If it doesn't have its own url, it will use the default Ajax url - set the defaultAjaxUrl attribute of the JSNLog configuration element.
  3. If you didn't set the default Ajax url, it finally falls back to /jsnlog.logger.

Use server side configuration to set the URL

The previous sections suggests you use server side configuration to set the URL. This way:

  • At the client side, log requests will be sent to your url.
  • At the server side, JSNLog will look for log requests sent to your url. Specifically, it will look for requests sent to:

    • The urls set on individual appenders; and
    • The default Ajax url, or if that is not given, /jsnlog.logger.

In principle, you could use client side configuration as well to set the url. However, at the server side, JSNLog would not know about your url, and so would not process requests to your url as log requests.

Cross domain requests (CORS)

You may want to send your log requests to a site with a domain that is different from the site where they originate. For example, you have multiple sites with their own domains (such as my-abc-domain.com, my-xyz-domain.com, etc.), and you want all log messages to go to a single common URL (such as mylogger.com).

By default, browsers do not allow JavaScript (such as jsnlog.js) to send AJAX requests to a domain different from the site domain.

To make cross domain requests possible, JSNLog implements the CORS protocol.

Allowed domains

For security reasons, you have to explicitly nominate the domains that JSNLog should accept requests from. All other domains will still be blocked.

To do this, set the corsAllowedOriginsRegex attribute of the JSNLog configuration element to a regular expression that matches all accepted domains (test your regular expression).

For example, this code allows requests from my-abc-domain.com, my-xyz-domain.com and all their sub domains, both over http and https:

<jsnlog corsAllowedOriginsRegex="^https?:\/\/([a-z0-9]+[.])*(my-abc-domain[.]com|my-xyz-domain[.]com)$" >
</jsnlog>
// Use in Configure method in Startup class
        var jsnlogConfiguration = 
  new JsnlogConfiguration {
      corsAllowedOriginsRegex="^https?:\\/\\/([a-z0-9]+[.])*(my-abc-domain[.]com|my-xyz-domain[.]com)$"
  };

Allowed request headers

If you add your own request headers to log requests, you have to configure these here as well. This cannot be done automatically, because adding the request headers is done using client side code, while the CORS protocol is implemented on the server.

The corsAllowedHeaders attribute of the JSNLog configuration element takes a comma delimited list of custom headers that are allowed in cross domain requests.

For example, this code allows the custom request headers X-MyHeader and X-MyHeader2:

<jsnlog corsAllowedOriginsRegex="^https?:\/\/([a-z0-9]+[.])*(my-abc-domain[.]com|my-xyz-domain[.]com)$" 
        corsAllowedHeaders="X-MyHeader, X-MyHeader2">
</jsnlog>
// Use in Configure method in Startup class
        var jsnlogConfiguration = 
  new JsnlogConfiguration {
      corsAllowedOriginsRegex="^https?:\\/\\/([a-z0-9]+[.])*(my-abc-domain[.]com|my-xyz-domain[.]com)$",
      corsAllowedHeaders="X-MyHeader, X-MyHeader2"
  };

JSNLog does not support CORS on IE8/9

IE8 and IE9 require additional code to support CORS. Because of their very low market share, JSNLog does not support CORS on these browsers. If you use CORS, then jsnlog.js running on these browsers will not send log messages to the server.