How to enable IP address logging with Log4Net

asp.net, log4net, logging

Solution

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.

Problem

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

Original source

Related problems