Loading...

All project content is available for reading, but you need to be a member of the project for Subversion checkout of source code, or to create/modify any information.
Login if you are a member. Apply here to request membership (open to all).

Log 4 Net Context Information

A Http Module that adds more information to the global log4net context, available to all loggers. Used to log more information about a given event, like the current request url, the user agent etc. Especially useful for error logging, where both user agent and the url of the request of the error can be important to reproduce the error.

Installing

Check out source, compile and copy BVNetwork.Log4NetContextInformation.dll to the bin directory of your site. Change web.config and episerverlog.config according to the configuration documentation below.

Requirements

log4net version 1.2.10.0 or later.

This version of the log4net assembly is shipped with EPiServer CMS 5.

Important! EPiServer CMS 4 uses an older version of the log4net assembly which is not compatible with this module. Porting this to work with EPiServer CMS 4 will require a major rewrite (as the %property{...} pattern is not available. MDC could be used but is not that flexible and cannot be evaluated on the fly.)

Configuration

Add the following line to the <httpModules> section of your web.config:

<add name="Log4NetContextInformation" 
     type="BVNetwork.Log4NetContextInformation.ContextInformationModule, BVNetwork.Log4NetContextInformation" />

Log4net Configuration

The added context information is available as properties in your output pattern in an appender. To render the value of one of these properties, use the following syntax: %property{NameOfProperty}, e.g. %property{CurrentRequestUserAgent}.

Example:

<layout type="log4net.Layout.PatternLayout">
  <param name="ConversionPattern" 
         value="%date|[UserAgent:%property{CurrentRequestUserAgent}] - [URL:%property{CurrentRequestUrl}] - [Referrer:%property{CurrentRequestReferrer}] %message%n" />
</layout>

Here is a full appender example:

<appender name="errorAppender" type="log4net.Appender.RollingFileAppender">
  <file value="errorLog.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" 
           value="%date|[UserAgent:%property{CurrentRequestUserAgent}] - [URL:%property{CurrentRequestUrl}] - [Referrer:%property{CurrentRequestReferrer} %message%n" />
  </layout>
</appender>

Available Context Properties

CurrentRequestUrl
Shows the url of the current request. "No HttpContext" if logged outside a request, or (null) if no Request is available
CurrentRequestUserAgent
The user agent of the current request. Use it to identify spiders and crawlers.
CurrentRequestReferrer
Shows the url of the referrer to the current request. "No HttpContext" if logged outside a request, or (null) if no Request is available.

If you need to add more properties, have a look at the source for one of the properties . Adding new properties is easy. Please commit your new properties so this module can be even more useful to others.

Source Code

Tickets

Tickets for this component:

No results

Report a bug
Feature request