0

Using RollingFileAppender with log4net, I am looking for a way to format the numbering of the files. Indeed, I would like to sort them and currently the alphabetical sorting is not in the reversed order of creation. For 12 log files I have :

  • logfile.txt
  • logfile.txt.1
  • logfile.txt.10
  • logfile.txt.11
  • logfile.txt.2
  • logfile.txt.3
  • logfile.txt.4
  • logfile.txt.5
  • logfile.txt.6
  • logfile.txt.7
  • logfile.txt.8
  • logfile.txt.9

Is there a way to specify a format "000", when rolling file ? (or any other way to have a alphabetical sorting inline with the produced order?)

Toto
  • 7,491
  • 18
  • 50
  • 72
  • You need to set the Conversion Pattern. See item 9 on following : https://stackify.com/log4net-guide-dotnet-logging/ – jdweng Mar 18 '23 at 15:00
  • @jdweng Conversion pattern is for formatting the log item itself, not the file name, no ? – Toto Mar 18 '23 at 18:01
  • You can add the date to the filename by changing the conversion pattern. See : https://stackoverflow.com/questions/1165084/log4net-rolling-daily-filename-with-date-in-the-file-name – jdweng Mar 18 '23 at 18:47
  • I am not using a date, rolling only on file size. I am looking for a similar way to set a pattern/format to the counter number appended to the file name. – Toto Mar 18 '23 at 21:45
  • Just showing it can be applied to filename. Here is complete list of options : https://logging.apache.org/log4net/log4net-1.2.13/release/sdk/log4net.Layout.PatternLayout.html – jdweng Mar 19 '23 at 09:08
  • Sorry I still don't figured out what you are trying to emphase. Could you post a full exemple ? – Toto Mar 19 '23 at 19:48
  • Simple. You can modify the filename using Conversion Pattern. You can see from link above you can add the date to the filename using Conversion Pattern. Likewise you can change the format of the number. – jdweng Mar 19 '23 at 23:10
  • Could you post a sample on the anwser section please ? – Toto Mar 20 '23 at 01:32
  • You need to use a rolling file appender similar to the one at following that add the process id : https://sitecore.stackexchange.com/questions/3371/change-the-name-of-the-output-log-file – jdweng Mar 20 '23 at 02:50
  • Not working for my use case. Could you post a quick answer with a snippet please ? – Toto Mar 20 '23 at 11:34
  • I do not have log4net installed so I can only refer you to other postings. Read a lot about filename and there seem to be three different types. Better to ask question at https://issues.apache.org/jira/browse/LOG4NET-611?jql=project%20%3D%20LOG4NET%20AND%20resolution%20%3D%20Unresolved%20ORDER%20BY%20priority%20DESC%2C%20updated%20DESC – jdweng Mar 20 '23 at 11:44
  • I looked at RollingFileAppender decompiled code, I it look like it is not possible to format the file index id. I am not sure how you arrived at that conclusion, could you rephrase your intuition ? – Toto Mar 21 '23 at 00:13
  • It is best to ask you questions at the apache site. You may need to pass a new parameter to the rolling file appender to be able to format the number or modify the library. – jdweng Mar 21 '23 at 02:34

0 Answers0