views:

495

answers:

1

I'm looking a way to enable IP logging with log4net in ASP.NET. I found one solution but it works at Application level. Any suggestions/practices how to log IP at session level?

Thanks

+6  A: 

In Application_BeginRequest, do

MDC.Set("addr", Request.UserHostAddress);

and then ensure that your PatternLayout contains %X{addr} somewhere in the pattern string.

Update: As Tadas has pointed out, in newer versions of log4net the equivalent is

ThreadContext.Properties["addr"] = Request.UserHostAddress;

coupled with %P{addr} in the pattern string.

Vinay Sajip
Great! Thanks. I just want to note that MDC class is deprecated (http://svn.apache.org/repos/asf/logging/site/trunk/docs/log4net/release/sdk/log4net.MDC.html) and forwards to ThreadContext.Properties.
Tadas
Nice - I had done this in a previous life, but had forgotten how. +1
Jarrod Dixon