tags:

views:

162

answers:

2

Please help me with this query in using log4net.

I am using log4net in mhy we application. I am facing issues in configuring log4net to log errors at user level.

That is, If user X logs in, I like to create file name X and all error for user X should be written in X.log. Siilarly if Y user logs in the log file should be in name of Y.log and the most important point to note is, they could log in concurrently.

I tried the luck by creating log files whose name would be framed dynamically as soon as the user logs in. But issue here, if they are not using the application at the same time, the log files are creeated with correct name and writing as expected, but if both users have active sessions, log file is created only for user who FIRST logged in and error of second user has been recorded in log file that is created for FIRST user.

Please help me in this.

+3  A: 

There has to be a better solution from this one, but you can change log4net configuration from code and even decide which config file to load - so you can do it in code, which is not as nice as editing an XML file.

so what you need to do, which is highly not recommended, is to create log4net configuration each time you call the logger static class, and do what needed based on the calling user.

again.. it doesn't feel right !

(and it will probably perform poorly).

another BETTER solution is to log everything to database (log4net supports it), with a user column, and then produce the logs from db....

Dani
+1 for logging to DB. Logging to a file for each user will not scale.
tvanfosson
+1 for logging to DB. IMO this is the right way to go. No concurrency/scalability issues and clean, out-of-the-box log4net usage.
Peter Lillevold
A: 

I tried this & got it:

1.Set the file name dynamically using GlobalContext (or ThreadContext) property which the user who logged in.

  1. Load config file using XmlConfigurator.Configure(ConfigFile).

  2. Use GetLogger(type())

But one of my friend is asking the following question:

User1 Logs in

Set GlobalContext = User1

Configure

Log

All is good. But what about:

User1 Logs in Set GlobalContext = User1 User2 Logs in Set GlobalContext = User2 Configure (from first login) Log something Configure (from second login) Log something else

Could you pls advise, I couldn't replicate it.

Raj