Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I configure log4net to write to %LOCALAPPDATA% on Windows XP and 7?

I've got an internal app which is using log4net for logging. I'd like the logs to be generated at %LOCALAPPDATA%\Vendor\App\application.log. Unfortunately, log4net is creating the log file at %APPDATA% instead. It's not a huge problem, because we really don't use roaming profiled here, but I don't like leaving little idiosyncrasies in my code if I can avoid it.

Any thoughts on how to get the file written to the location I specified without configuring log4net programattically and using pinvoke to get the path for XP?

Here's the appender section of my config file if it's any help:

<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>
like image 848
bshacklett Avatar asked Feb 26 '13 19:02

bshacklett


People also ask

Where does log4net write to?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation. The different log files are described in Services logs.

How do I use log4net net 6?

Just create a log4net. config file with a log file as an appender, then add two using statements and a single line of code to the new . NET 6 hosting model: //Program.


1 Answers

VERY late to the party here but I just came across this and the found the answer.

Seems you can use log4net.Util.PatternString to insert environment variables into the file path. So the OP's example becomes:

<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>

Add type="log4net.Util.PatternString" to the file element and then specify the %env pattern specifier followed by the environment variable name in curly brackets.

like image 160
MarkD Avatar answered Oct 21 '22 11:10

MarkD