How to configure log4net to write to% LOCALAPPDATA% in Windows XP and 7?

I have an internal application that uses log4net for logging. I would like the logs to be generated in %LOCALAPPDATA%\Vendor\App\application.log. Unfortunately, log4net creates a log file in %APPDATA%. This is not a huge problem, because we really do not use roaming profiled here, but I do not like to leave slight differences in my code if I can avoid this.

Any thoughts on how to get the file recorded to the location I specified without having to configure the log4net program and use pinvoke to get the path for XP?

Here's the appender section of my configuration file if it helps:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="${LOCALAPPDATA}\Vendor\App\application.log" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="100KB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger%newline%message%newline" />
  </layout>
</appender>
+7
source share
2

, , .

Log4net , appdata, localappdata. , , System.Environment.GetEnvironmentVariables(), - .

+8

, .

, log4net.Util.PatternString . , OP :

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="%env{LOCALAPPDATA}\Vendor\App\application.log" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="100KB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger%newline%message%newline" />
  </layout>
</appender>

type = "log4net.Util.PatternString" , % env, .

, -!

0

All Articles