Wednesday, September 27, 2006


Adding Custom Logging Properties to your Log4Net configuration

In the Log4Net release notes there's a note about now to create your own custom pattern element in the logging configuration, so that, e.g., your conversion pattern might look like:

"%-5level %logger - %myConverter - %message%newline"

Maybe someone can comment on why this would be better than what I will recommend. Anyways, if you take this approach and want to include information about the actual logging event, you should know two things:
1) subclassing PatternLayoutConverter instead of PatternConverter will give you a Convert method that includes a LoggingEvent object.

2) The state object passed to PatternConverter.Convert is actually a LoggingEvent object!

I found it easier to create a new appender, which simply added objects to the LoggingEvent's properties collection.

Here's the appender class:

public class PropertiesExpanderAppender : ForwardingAppender
protected override void Append(LoggingEvent loggingEvent)
if (null != HttpContext.Current)
HttpContext context = HttpContext.Current;
loggingEvent.Properties["USER_IP"] = context.Request.UserHostAddress;
loggingEvent.Properties["REQUEST_URI"] = context.Request.Url.PathAndQuery;

if (null != UserProperties.CurrentUser)
UserProperties userProperties = UserProperties.CurrentUser;
loggingEvent.Properties["USER_EMAIL"] = userProperties.UserName;
loggingEvent.Properties["USER_NAME"] = userProperties.Name;
loggingEvent.Properties["USER_ID"] = userProperties.UserId;

// Delegate to base class which will forward

Here are the important snippets from the log4net configuration:

<appender name="PropertiesExpanderAppender" type="NameSpace.PropertiesExpanderAppender">
<appender-ref ref="RollingLogFileAppender" />
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date ... %P{USER_IP} %P{REQUEST_URI} %P{USER_EMAIL} %P{USER_NAME} ..." />
<level value="ALL" />
<appender-ref ref="PropertiesExpanderAppender" />

Note the %P{USER_IP} pattern elements, which correspond to the properties added by the forwarding appender. Now you can add as many logging elements as you want, without having to add a bunch of classes.

I find it easier and less code to simply do this in your code (maybe in global.asax.cs or other appropriate location).

log4net.ThreadContext.Properties["Url"] = url.ToString();

Then you can reference that property in your log4net.config file via "%X{Url}".

No need to write a separate class.
Hey, nice! Thanks Phil.
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?