13

How can I log to special folders (e.g. %APPDATA%) using the app.config file?

I can do it programmatically, but I need to be able to use the app.config file for configuration. I have seen a post of using %envFolderPath.It is not available in the latest released version, but only in their latest code.

Below is the code that I setting the log to special folders programmatically.

public void ExampleLog
{
    XmlConfigurator.Configure();

    var fileName = GetFileName();
    var appender = new log4net.Appender.RollingFileAppender
    {
        Layout = new log4net.Layout.PatternLayout("%d - %m%n"),
        File = fileName,
        MaxSizeRollBackups = 10,
        MaximumFileSize = "100MB",
        AppendToFile = true,
        Threshold = Level.Debug
    };

    appender.ActivateOptions();
    BasicConfigurator.Configure(appender);
}

private static string GetFileName()
{
    const string subPath = "MySubFolder";
    var path = String.Format(@"{0}\{1}", Environment.GetFolderPath  (Environment.SpecialFolder.CommonApplicationData), subPath);
    const string logName = "Log.txt";
    return Path.Combine(path, logName);
}
Ryan Gates
  • 4,501
  • 6
  • 50
  • 90
user9969
  • 15,632
  • 39
  • 107
  • 175
  • 1
    possible duplicate of [How to specify common application data folder for log4net?](http://stackoverflow.com/questions/468989/how-to-specify-common-application-data-folder-for-log4net) – Anthony Mastrean Jun 16 '11 at 03:47

2 Answers2

13

Pretty sure the syntax for this is available in the current release.

<file type="log4net.Util.PatternString" value="%env{APPDATA}\\MyApp\\Log.txt" />

If you need something more, you can look into option of subclassing the PatternString class, as described here: Log4Net can’t find %username property when I name the file in my appender

Community
  • 1
  • 1
Bryan Batchelder
  • 3,627
  • 21
  • 17
  • Sorry for late response. Thanks! – user9969 Dec 22 '09 at 21:56
  • I don't think this is a good idea, because on Vista and Windows 7, %APPDATA% resolves to the user's *roaming* directory, which means the log files will be synced to the domain server and downloaded each time the user logs in. See the comment here: http://stackoverflow.com/questions/1572934/where-to-store-an-application-log-file-on-windows/1573015#1573015 – GuyBehindtheGuy Aug 25 '10 at 15:07
  • 2
    If you're concerned about the time to synch roaming data, then rather use %LOCALAPPDATA% (the non-roaming equivalent). I think that would usually be better in any case for log files, regardless of how big they get. – Disillusioned Oct 05 '11 at 11:18
4

Check out the RollingFileAppender configuration sample on the log4net docs (for all the other parameters).

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="log.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="100KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.SimpleLayout" />
    </layout>
</appender>

I've referenced environment variables (including special folders) with the basic log4net variable format ${NAME}. And the file tag doesn't need to have the PatternLayout specified, it's implied.

<file value="${APPDATA}\log.txt" />
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188