1

I am trying to write log file per user separately

For this I am setting the property of file name as a current user id and configuring log4net like this

log4net.GlobalContext.Properties["userid"] = userId; 
log4net.Config.XmlConfigurator.Configure();

This is all working good, it creates new log file per user and logs all the activities by the user in their separate files.

Problem starts when multiple users request service simultaneously, it then starts putting part of logs of one user to another users file, so all logs get mixed up in the simultaneous users files.

I am getting the reason that global settings are getting overridden by user2 while user1 is in progress so user1 starts writing logs to user2's file as the settings are now overridden.

I tried log4net.LogicalThreadContext and log4net.ThreadContext also but they also didn't work.

Is there any workaround for this?

4

1 回答 1

0

Log4net 只有一种配置。因此,您无法通过为每个用户获取不同的配置来解决此问题。但是,您可以记录用户名,或创建以您的用户命名的记录器。然后,您可以为每个用户制作不同的附加程序,并将输出记录到不同的文件中。而且您只需调用log4net.Config.XmlConfigurator.Configure();一次。

string userid = (...) ;
ILog log = LogManager.GetLogger(userId);


<log4net>
<appender name="<userid1>" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
  ....
</appender>
<appender name="<userid2>" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
  ....
</appender>
<logger name="<userid1>">
  <level value="ALL"/>
  <appender-ref ref="<userid1>"/>          
</logger>
<logger name="<userid2>">
  <level value="ALL"/>
  <appender-ref ref="<userid2>"/>          
</logger>
....
于 2013-06-03T13:41:08.153 回答