Properties in log4net config

Another little log4net gem! You are probably aware of the use of property in conversion patterns in log4net using the PatternLayout, but did you know you could use them in configuration? Well I didn’t..

My goal was to push a rolling log file path file into the config file, so that we could avoid having to maintain multiple config files across services. So choosing the global context for properties (there are numerous contexts) I just added the file path before calling Configure in my case using the XmlConfigurator:

1 log4net.GlobalContext.Properties["LogFilePath"] = logFilePath;

In config I can reference this named property using the conversion pattern syntax in the file value:

1 ... 2   <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> 3     <file type="log4net.Util.PatternString" value="%property{LogFilePath}"/> 4 ... 5

The key part to note is the type of “log4net.Util.PatternString” associated to the file element allowing the conversion syntax to be interpolated – pretty sweet.

Add comment

Loading