log4net – another way to change log file location on runtime

In my previous post I wrote about the simplest way of specifying logging location, which limited the solution only to environment variables set on a particular machine. However if you wanted to expand on all types of special folders available in Windows then we will have to look for a different solution, fortunately support for that is also available within log4net.

Difficulty level of this solution is a bit high, but it provides nearly 60 locations to choose from, as it uses “Environment.SpecialFolder” enumeration.

This is a two step process, first we need to create a derived PatternConverter class, which will take the token and map it to the specific folder and write back the value.

Now we have the required implementation, it’s time to integrate it in the configuration flow.

Run and test the application, voila, success again.

Basically the %folder tells it to look at the converter called folder which points it to the SpecialFolderPatternConverter class. It then calls Convert on that class, passing in the CommonApplicationData.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.