How to log a separate log file in a stream using log4net

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"> <!--need to replace LogDir in code like this: log4net.GlobalContext.Properties["LogDir"] = "c:\programdata\myapp"--> <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() { // this log message should be sent to a log file named after the current thread System.Threading.Thread.CurrentThread.Name // Examples: mylogfile_bob.log, and mylogfile_fred.log, etc... Log.Info("Starting work on thread " + System.Threading.Thread.CurrentThread.Name); // do long running work here } } 

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.

+4
source share
1 answer

Please try the following:

http://geekswithblogs.net/rgupta/archive/2009/03/03/dynamic-log-filenames-with-log4net.aspx

If you need to create dynamic log names using log4net, then you can use the following configuration

 <appender name="RollingFileAppenderV1" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="F:\HornetFeed\%property{LogName}" /> <appendToFile value="true" /> <rollingStyle value="Size" /> ... <filter type="log4net.Filter.PropertyFilter"> <Key value="Version" /> <StringToMatch value="1" /> ... <= Note the "%property{LogName}" syntax 

Note the% {LogName} property, this is the log4net property that we can set at runtime using C # code.

 log4net.GlobalContext.Properties["LogName"] = "file1.log"; 

Remember to set the GlobalContext properties before creating the log4net logger. i.e. before this call:

 log4net.ILog log = LogManager.GetLogger(typeof(Program)); 

Then:

  ///Helper method to log errors: internal static void LogError(Exception ex) { string state = "1"; if (log4net.ThreadContext.Properties["Version"] != null) state = log4net.ThreadContext.Properties["Version"].ToString(); log4net.ThreadContext.Properties["Version"] = "0"; logger.HandleException(ex, "Error"); log4net.ThreadContext.Properties["Version"] = state; } 

'Hope that helps

+1
source

Source: https://habr.com/ru/post/1400436/


All Articles