5

I am using %property% to set the output file path in the Log4Net configuration file. A log file will be created in the APP data folder every time when the application launches. I am using the Composite rolling style for rolling the files.

But now my requirement is to roll/change the file path based on some user interactions in the application. How can I achieve this, can anyone suggest me to achieve this.

  1. How to roll the file in c# code. The expected behavior is similar to rolling based on Size and date.
  2. What is the c# code to change the output file path in Log4Net in between program execution?

Please let me know if my requirement is not clear.

Thanks.

Venkat
  • 2,549
  • 2
  • 28
  • 61

1 Answers1

4

You have 2 questions:

  1. I'm not aware if this is possible. I guess the roll to next name is private in the rolling file appender. You can look in the src to see if you can access it. If not you can inherit from the RollingFileAppender and add your own implementation. You can get the appenders on runtime by:

code:

  LogManager.GetRepository().GetAppenders();
  1. Using a property is the right way. Your configuration should look like this:

config:

 <appender name="YourAppender" type="log4net.Appender.RollingFileAppender"> 
    <file type="log4net.Util.PatternString" value="~/App_Data/%property{LogName}" />
 </appender>

Important is the type="log4net.Util.PatternString". Set the property before initializing log4net.

log4net.GlobalContext.Properties["LogName"] = name;
Peter
  • 27,590
  • 8
  • 64
  • 84
  • Thanks for your answer , Could you please share me the source location to check your suggestion. It will be helpful to determine whether the method is private or public. – Venkat Oct 11 '17 at 08:57