Yes, this question is similar to: How to connect to individual files in a stream using Log4Net? except that I do not know the number of threads or their names until runtime. In my Windows application, a thread is created for each user in order to do lengthy work for that user. I want a separate log file for each user / stream.
- What does the log4net configuration file look like (if it can be used for this type)?
- What will be the code for using the registrar?
- When will I call
log4net.Config.XmlConfigurator.Configure()?
(Please provide details on how to complete the registration.)
Here is a config example (I cannot get the thread_name property to work with multiple threads):
<log4net debug="false"> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="%property{LogDir}\logs\mylogfile_%property{thread_name}.log" /> ...
And the code:
public class MyMultiThreadedClassForUsers { private log4net.ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public void Start() { log4net.GlobalContext.Properties("LogDir") = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) log4net.Config.XmlConfigurator.Configure() List<IUser> users = GetAllUsersFromDB(); foreach (IUser user in users) { System.Threading.Thread t = new System.Threading.Thread(CallBackMethod); t.Name = user.FirstName; t.Start(); } } private void CallBackMethod() {
If this is not easy to do with log4net, I can switch the logging frameworks to Nlog and use their% threadname keyword as part of the log file name that is stored in the configuration file.
source share