Oleg  Ignatov
Oleg Ignatov

Reputation: 865

File name with DateTime stamp

I have the following RollingFileAppender config:

  <appender name="appender.VTBGPRS" type="log4net.Appender.RollingFileAppender">
    <rollingStyle value="Size" />
    <file value="mylog" />
    <staticLogFileName value="false" />
    <appendToFile value="true" />
    <maximumFileSize value="100KB" />
    <maxSizeRollBackups value="20" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %logger{1} - %message%newline" />
    </layout>
  </appender>

And I have files named mylog, mylog.1, mylog.2, etc.
I want to add date time stamp to the file name. For example mylog-2012-07.

I can't use datePattern because rollingStyle is set to Size.

How can I add date time stamp to the file name?

Upvotes: 1

Views: 2597

Answers (2)

Chanon Onman
Chanon Onman

Reputation: 21

You can use rollingStyle as Composite. It will split logs base on size and date.

  <appender name="appender.VTBGPRS" type="log4net.Appender.RollingFileAppender">
    <rollingStyle value="Composite" />
    <datePattern value="-yyyyMMdd'.txt'" />
    <file value="mylog" />
    <appendToFile value="true" />
    <maximumFileSize value="100KB" />
    <maxSizeRollBackups value="20" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %logger{1} - %message%newline" />
    </layout>
  </appender>

This will generate logs as: myLog.1, mylog.2, ... during current day and they will be changed to myLog-20141024.txt.1, myLog-20141024.txt.2, ... at the end of the day. You can try it by changing system date to the future to see this log rolling mechanism.

Upvotes: 2

chrismead
chrismead

Reputation: 2243

If you can change things at runtime, you can use something like:

<file type="log4net.Util.PatternString" value="%property{FileName}" />

And in code:

log4net.GlobalContext.Properties["FileName"] = somethingWithADateInIt;

and format the somethingWithADateInIt string using whatever is relevant in your language.

Upvotes: 0

Related Questions