views:

79

answers:

2

Hi,

I am searching for the best way for track user on a web site based on asp.net.
We are using log4net to log some business actions (enter on this page, click on button, etc.). But for multiple users, log file cannot be read easily.

So I need add a property 'UserName' on the config file like this :

 <conversionPattern value="%date [%thread] - %property{UserName} - %-5level  - %logger - %message%newline"/>

Do you hae any idea about the way to set 'UserName' ?

thanks for your help

A: 

Following property is there

   %username the Windows identity of user making the log entry; slow 

check for more detail : http://www.beefycode.com/post/Log4Net-Tutorial-pt-4-Layouts-and-Patterns.aspx

check this article : Capture Web Events in log4net

Pranay Rana
That would only work if they are using integrated windows authentication, otherwise you'll just get the identity that the application is running under.
R0MANARMY
A: 

You can define a username (or any other) property like this:

this.SetThreadContext("UserName", userName);

assuming userName holds the name of the user.

Edit (What you really want to do):

Write a wrapper for log4net (with interface) and make some method e.g. SetUserName like this:

public void SetUserName(Func<string> getUserName)

This method you call at application start and you pass some method that returns the currently logged in user (from the HttpContext). You will need to store the delegate somewhere, maybe a static variable. Then you have methods for logging at various levels and every single method calls the delegate (Func<string> getUserName) and sets the thread context property as shown above. This way you do not have to set the thread context property manually before every call to the logger...

Stefan Egli