10

I have an appender setup like this

<appender name="Scheduler_Appender" type="log4net.Appender.RollingFileAppender">
   <file value="c:\temp\ApplicationLog.txt"/>
   <rollingStyle value="Date"/>
   <datePattern value="yyyyMMdd"/>
   <appendToFile value="true"/>
   <staticLogFileName value="true"/>
   <layout type="MinLayout">
    <locationInfo value="true"/>
   </layout>
</appender>

When the log file first gets created the file name is simply ApplicationLog.txt this is correct.

However when the logging rolls - the filename that gets generated is ApplicationLog.txt20100323 (for example), and not ApplicationLog20100323.txt

How can I change the configuration so files are rolled to [FileName][Date].[ext] rather than [FileName].[ext][Date]

Thanks

Update

I tried this, just some experiment, but now its generating files with a .pxp extension for some reason...

<appender name="Scheduler_Appender" type="log4net.Appender.RollingFileAppender">
            <file value="c:\temp\Scheduler"/>
            <rollingStyle value="Date"/>
            <datePattern value="yyyyMMdd.txt"/>
            <appendToFile value="true"/>
            <staticLogFileName value="false"/>
            <layout type="MinLayout">

            </layout>
        </appender>
JL.
  • 78,954
  • 126
  • 311
  • 459
  • About the "pxp" extension, maybe the "t" of "txt" is interpreted. I had the problem with "log" extension. I escaped the "g" and it is fine now ("yyyyMMdd-HHmmss.lo\g") – GôTô May 17 '13 at 08:16
  • Related post - [Log4net rolling daily filename with date in the file name](https://stackoverflow.com/q/1165084/465053) – RBT Jul 15 '21 at 04:13

3 Answers3

25

I believe the setting you want is PreserveLogFileNameExtension:

<appender name="Scheduler_Appender" type="log4net.Appender.RollingFileAppender">
     ...
     <PreserveLogFileNameExtension value="true" />

Note: this property is not available in the currently released binary (version 1.2.10.0) so you would have to grab the latest source and go from there.

Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131
7
<appender name="cheduler_Appender" type="log4net.Appender.RollingFileAppender">
            <file value="c:\temp\Scheduler"/>
            <rollingStyle value="Date"/>
            <datePattern value="yyyyMMdd'.txt'"/>
            <appendToFile value="true"/>
            <staticLogFileName value="false"/>
            <layout type="MinLayout">

            </layout>
        </appender>
JL.
  • 78,954
  • 126
  • 311
  • 459
  • this way your current log file will not have an extension, has it? – Stefan Egli May 14 '10 at 11:18
  • yes it has amazingly - this is because staticLogFileName is set to false. But I know what you mean, this is the last time I use log4net, next time its nLog. – JL. May 14 '10 at 11:43
  • there is a helper class in this post which has a method to find filename http://stackoverflow.com/a/11694426/1060656 – aked Jul 27 '12 at 19:29
4

Try this one, it will create log file based on current date

log filename (change the format accordingly) : "log-[15-02-2017].[15.57.10].log"

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <file type="log4net.Util.PatternString">
        <conversionPattern value="log-[%date{dd-MM-yyyy}].[%date{HH.mm.ss}].log" />
      </file>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p [%logger:%L] - %m%n" />
      </layout>
    </appender>
Community
  • 1
  • 1
Narottam Goyal
  • 3,534
  • 27
  • 26