Can I change the log file name for the file adapter while the application is running? This will be done several times / day.
I will try to dwell on my own: the application writes the firmware to the device. All devices that the user has already worked with are in the grid. The user can start a new recording wizard or resume or restart an action on an already running device. What I would like to do is keep a log of all the steps the user takes for a particular device.
For example: when the user is working on the device AB0124, I want to write to the log file AB0124.log. When he finishes working on this device and starts working on the XY5618 device, I want to register these actions in XY5618.log
I read that you can use a context property ( here and here and many other messages), but you must set the property before creating the registrar. Thus, instead of creating a registrar in a class, I create it in my method after setting the property. But still nothing is being recorded.
When I set the file name hardcoded in config, it works. Did I miss something here?
Log4Net.config:
<appender name="StepsLogAppender" type="log4net.Appender.FileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="INFO"/> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <file type="log4net.Util.PatternString" value="%property{LogPathModifier}" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date{dd/MM/yyyy - HH:mm:ss} - %message%newline" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="StepsLogAppender" /> </root>
WITH#
public void WriteStepInfo(string device, int step) { log4net.ThreadContext.Properties["LogPathModifier"] = string.Format("D:\\StepsDevice_{0}.txt", device); var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); log.Info(string.Format("Device {0} - step {1}.", device, step)); }
And in AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
source share