Reputation: 4220
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
Upvotes: 15
Views: 12159
Reputation: 7821
With log4net 1.2.11 (Oct 2011) you add the following to your pattern layout:
%aspnet-request{REMOTE_ADDR}
Or for the current user:
%aspnet-request{AUTH_USER}
See https://issues.apache.org/jira/browse/LOG4NET-87 for more info on the new asp.net patterns converters (%aspnet-cache, %aspnet-context, and %aspnet-request).
Upvotes: 20
Reputation: 99365
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.
Upvotes: 19