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>
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With