user3729645
user3729645

Reputation: 147

How to Log Client Ip, Browser Name and User Name using Log4net in asp.net when multiple clients are using application at same time?

I want to log Client IP Address, Browser Name and Windows Authenticated User Name using log4net library.

I'm trying in this way, but its not working.

So Please help me out.

Server Side Code:

log4net.GlobalContext.Properties["Hostname"] = GetIP();
log4net.GlobalContext.Properties["Browser"] =  HttpContext.Current.Request.Browser.Type;
log4net.GlobalContext.Properties["username"] = HttpContext.Current.Request.LogonUserIdentity.Name;

Web.Config Code:

This works fine. But If multiple Users working simultaneously, It logs same IP and other information. Reason might be that I'm setting GlobalContext properties. So all info is overwritten with the latest User logged in.

So It does not fulfill my requirement. I want to log each and every logged in user's info, even if they are using web application simultaneously.

Upvotes: 0

Views: 1644

Answers (1)

Mike Hixson
Mike Hixson

Reputation: 5189

You can use a PatternLayout in your configuration and reference the values you want from HttpRequest like so:

%aspnet-request{key}  

Where {key} is the key of a property in HttpRequest.Item that you want to grab. You can also use it with no key to dump all the properties. For example you could put in the following to get the IP Address:

%aspnet-request{REMOTE_ADDR}

More documentation here: http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html

Upvotes: 1

Related Questions