16

In my app.config I put

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="%programdata%/log-file.txt"/>

but it didn't work. Any ideas?

1
  • 1
    Yeah, its not expanding %programdata%.
    – user1228
    Jul 26, 2010 at 19:04

4 Answers 4

28

The log4net syntax for expanding environment variables is "${Variable}" e.g.

<file value="${LOCALAPPDATA}\GojiSoft\GojiLog\log.txt" />
10
  • is this syntax log4net specific? will it work in other settings?
    – Jader Dias
    Jul 26, 2010 at 19:08
  • 3
    @Jader This is specifically implemented in log4net.
    – Tim Lloyd
    Jul 26, 2010 at 19:10
  • 1
    While your example works fine, ${PROGRAMDATA} doesn't work. But I think it's some kind of permission problem.
    – Jader Dias
    Jul 26, 2010 at 19:13
  • 2
    @Jader The owner of the process needs to have permission to write to the specified location.
    – Tim Lloyd
    Jul 26, 2010 at 19:13
  • do you have link to the reference page which talks about this log4net syntax?
    – Jader Dias
    Jan 13, 2011 at 17:16
21

Resurrecting an old thread here, but I encountered the same issue and thought I'd share.

${PROGRAMDATA}, as discussed in the comment thread of the other answer, didn't work for me (same as for OP). However, I saw a comment somewhere about it being case sensitive. Tried ${ProgramData} and sure enough, it worked fine.

4
  • Had the same experience. I had to use LOCALAPPDATA all uppercase for it to work. LocalAppData fails. Nov 18, 2013 at 20:47
  • Hmmm...must be a difference in versions of something - whether it's Windows, compilers, log4net itself, I'm not sure though.
    – teleute00
    Nov 19, 2013 at 0:18
  • Pay attention "ProgramData" means every user shares the same log.
    – Polluks
    Jan 22, 2019 at 15:11
  • and I tried out all kind of path (localappdata, appdata, programdata) with different cases and none works. It always ends up creating folders like "${ProgramData}". in the bin folder. So annoying
    – Welcor
    Jan 11, 2020 at 21:27
2

Use ${APPDATA} instead of ${LOCALAPPDATA}

0

We have found that when using Log4Net with DotNetCore and the Microsoft.Extensions.Logging.Log4NetProvider extension method environment variables are not passed through. This is because the log4net configurator is configured by parsing the log4net config file into XML before passing it to the log4net configurator which bypasses the environment variable substitution. However, you can override properties by doing the following:

hostBuilder.ConfigureLogging(logBuilder =>
            {
                var logsPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                logBuilder.AddLog4Net(new Log4NetProviderOptions
                    {
                        PropertyOverrides = new List<NodeInfo>
                        {
                            new NodeInfo
                            {
                                XPath = "//appender[@name='rollingFile']//file",
                                Attributes = new Dictionary<string, string>
                                {
                                    {
                                        "value", Path.Combine(logsPath, "Application.log") 
                                    }
                                }
                            }
                        }
                    });
            });
<log4net>
  <root>
    <level value="INFO" />
    <appender-ref ref="console" />
    <appender-ref ref="rollingFile" />
  </root>
  <appender name="console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level - %message%newline" />
    </layout>
  </appender>
  <appender name="rollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="Application.log" /> <!--This is overidden at run time. Based on the appender name: rollingFile.-->
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="5" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
    </layout>
  </appender>
</log4net>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.