Documentation Menu

Sending log requests to a non default URL

By default, the jsnlog.js library sends all log requests to the URL /jsnlog.logger.

To send all log requests to another URL, the following needs to be done:

  1. Configure jsnlog.js so it sends to the new url.
  2. If needed, allow cross domain requests (CORS).
  3. Check the handlers in your web.config.
  4. If you use OWIN, check its configuration.

1. Configure jsnlog.js

To change the default URL that log requests are sent to:

  • In your server side configuration - set the defaultAjaxUrl attribute of the JSNLog configuration element.
  • Or in JavaScript - set the defaultAjaxUrl option of the global JL object.

You can further override the default for each Ajax Appender:

  • In your server side configuration - set the url attribute of the AjaxAppender.
  • Or in JavaScript - set the url option of the AjaxAppender.

2. Allow cross domain requests

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, and you want all log messages to go to a single common URL.

An issue is that 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.

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

For a working example, see the JSNLogDemo_Log4Net_CORS project in the simple working demos solution on GitHub.

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.

3. Check web.config

Check the handlers in the <system.web> and/or <system.webServer> sections of your web.config file. These contain a wildcard matching the url of log requests.

4. Check OWIN configuration

If you do not use OWIN, skip this step.

The OWIN middleware component looks at the URL of all incoming requests to see if it is a logging request. If it is, it processes the logging information in the request and then short circuits it, so it doesn't travel on to the downstream components.

If you changed the URL where log requests are sent, you want to make sure that the OWIN middleware component looks for the right URL.

If you use server side configuration to configure that new URL, this is all taken care off. The OWIN middleware component looks at the server side configuration to find out what URLs to look for.

However, if you configure the new URL in JavaScript, the OWIN middleware component has no way of knowing about the new URL.

To solve this, simply set the defaultAjaxUrl attribute on the JSNLog configuration element. This will tell the OWIN middleware component about the new URL.