35

I want log4net to write log files (using RollingFileAppender) to a subfolder of the common application data folder (e.g. C:\Documents and Settings\All Users\Application Data\Company\Product\Logs).
However, on Win XP, there is no environment variable that specifies this folder. We have %ALLUSERSPROFILE%, we have %APPDATA%, but there is nothing like %ALLUSERSAPPDATA%.
Programatically, I could use Environment.SpecialFolder.CommonApplicationData, but I need to put it in the log4net config, something like this:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
   <file value="%ALLUSERSAPPDATA%\Company\Product\Logs\error.log" />
</appender>

OK, we could define this in our setup, but maybe someone comes up with a better idea?

Gavin Miller
  • 43,168
  • 21
  • 122
  • 188
user57474
  • 523
  • 1
  • 5
  • 10
  • please correct the question title spelling "specifiy" – Jader Dias Jan 11 '11 at 18:43
  • Per the link in @codeulike's answer, as of 1.2.11, you can simply use a PatternString expression like [`%envFolderPath{CommonApplicationData}`](http://stackoverflow.com/questions/1535736/log4net-how-can-i-change-the-file-location-programmatically-c/1535998#1535998). – Asherah Aug 08 '14 at 04:55

6 Answers6

34

We just use this:

<param name="File" value="${ALLUSERSPROFILE}/Company/Product/Logs/error.log"/>

It works great.


This line can simply be inserted into your current appender configuration:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
   <param name="File" value="${ALLUSERSPROFILE}/Company/Product/Logs/error.log"/>
</appender>
Gavin Miller
  • 43,168
  • 21
  • 122
  • 188
pduncan
  • 1,330
  • 2
  • 15
  • 26
  • This solution will not work on Win Xp, since there is no valid resolving for ${ALLUSERSPROFILE}. Solution for all systems (Win Xp and following) is in accepted answer from here http://stackoverflow.com/questions/1535736/how-can-i-change-the-file-location-programmatically/1535998#1535998 – user2126375 Apr 14 '14 at 19:17
  • 2
    In Windows XP, ALLUSERSPROFILE does exist and defaults to C:\Documents and Settings\All Users, so this should work, unless you have a permissions issue. – pduncan Jun 11 '14 at 13:12
12

Here's the full code from the log4net mailing list that pilif linked to:

Basically the method is to implement a custom pattern converter for the log4net config file.

First add this class to your project:

public class SpecialFolderPatternConverter : log4net.Util.PatternConverter
{
    override protected void Convert(System.IO.TextWriter writer, object state)
    {
        Environment.SpecialFolder specialFolder = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), base.Option, true);
        writer.Write(Environment.GetFolderPath(specialFolder));
    }
}

Then set up the File parameter of your FileAppender as follows:

<file type="log4net.Util.PatternString">
    <converter>
      <name value="folder" />
      <type value="MyAppName.SpecialFolderPatternConverter,MyAppName" />
    </converter>
    <conversionPattern value="%folder{CommonApplicationData}\\SomeOtherFolder\\log.txt" />
</file>

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 (or whatever) enum value.

Apparently in the next release of log4net (1.2.11) there will be a simpler method, as described here.

Community
  • 1
  • 1
codeulike
  • 22,514
  • 29
  • 120
  • 167
12

This posting on the log4net mailinglist explains how you can define your own path replacement variables.

pilif
  • 12,548
  • 5
  • 34
  • 31
  • Thanks, this gave me the right idea. I will define a property and use a PatternString for the folder in the config file. – user57474 Jan 22 '09 at 13:51
  • You can use solution from accepted answer from here http://stackoverflow.com/questions/1535736/how-can-i-change-the-file-location-programmatically/1535998#1535998 . This solution is working on Win Xp and all folowing systems – user2126375 Apr 14 '14 at 19:20
  • 1
    Can you summarize the main ideas from the link? – Ryan Gates Jun 04 '14 at 16:42
  • Link-only answer can use any value once the link goes dead. Please summarize the solution provided in that linked resource. – O. R. Mapper Apr 28 '15 at 06:41
4

In the current log4net version (2.0.8.0) you could simply use

<file value="${ProgramData}\myFolder\LogFiles\" /> for C:\ProgramData

${LocalAppData} for C:\Users\user\AppData\Local\

and ${AppData} for C:\Users\user\AppData\Roaming\

Coden
  • 2,579
  • 1
  • 18
  • 25
3

Complete and working solution - content of my Log4net.config file. In actual version of Log4Net there is no need for writing own pattern converter anymore

<?xml version="1.0"?>
<log4net>
  <root>
    <level value="INFO" />
    <appender-ref ref="LogFileAppender" />
  </root>
  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
    <file type="log4net.Util.PatternString" value="%envFolderPath{CommonApplicationData}\\MyProject\\Logs\\log.txt" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="100MB" />
    <layout type="log4net.Layout.PatternLayout">
      <ConversionPattern type="log4net.Util.PatternString" value="%%-5p %%d{yyyy-MM-dd HH:mm:ss} - %%m%%n" />
    </layout>
  </appender>
</log4net>
user2126375
  • 1,594
  • 12
  • 29
1

Now (in 2018 FEB) as per log4net version 2.0.8.0.

You can use without any converter to get Environment Variables as follows.

<file type="log4net.Util.PatternString" value="%envFolderPath{CommonApplicationData}\\mylogfile.txt" />

Refer: log4net.Util.PatternString class documentation for more details.

Mohamed Navas
  • 592
  • 7
  • 16