Question

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?

Was it helpful?

Solution

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

<file value="${LOCALAPPDATA}\GojiSoft\GojiLog\log.txt" />

OTHER TIPS

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.

Use ${APPDATA} instead of ${LOCALAPPDATA}

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top