Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process Id not coming in log4net log file name

Tags:

c#

log4net

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?

like image 545
Sandeep Avatar asked Mar 21 '13 09:03

Sandeep


1 Answers

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" />
like image 66
sgmoore Avatar answered Nov 03 '22 01:11

sgmoore