I'm trying to have the process Id information in my log file name so that a new log file is created every time the application is relaunched. I'm logging from two projects into the same file. This is my config.
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="NewLogForEveryRun" type="log4net.Appender.RollingFileAppender">
<file value="c:\\testLogs\\TwoProjects-[%processid].txt" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<logger name="ClassLibrary1">
<level value="ERROR" />
<maximumFileSize value="256KB" />
<param name="Threshold" value="ERROR"/>
<appender-ref ref="NewLogForEveryRun" />
</logger>
<logger name="ClassLibrary2">
<level value="ERROR" />
<maximumFileSize value="256KB" />
<param name="Threshold" value="ERROR"/>
<appender-ref ref="NewLogForEveryRun" />
</logger>
</log4net>
</configuration>
I'm mentioning %processid
to get the Process Id info in the log file name, but when the application runs, it creates the log file name as TwoProjects-[%processid].txt
. The actual process Id is not shown in the file name. What could be the reason for this?
By default your file value is not parsed or processed unless you specify say it contains a pattern.
Solution is to change the line
<file value="c:\\testLogs\\TwoProjects-[%processid].txt" />
to
<file type="log4net.Util.PatternString" value="c:\\testLogs\\TwoProjects-[%processid].txt" />
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