문제

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?

도움이 되었습니까?

해결책

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

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

다른 팁

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top