Log4net rolling daily filename with date in the fi

2019-01-04 19:03发布

I would like to have files named for example:

dd.mm.yyyy.log

How is this possible with log4net?

8条回答
冷血范
2楼-- · 2019-01-04 19:28

The extended configuration section in a previous response with

 ...
 ...
 <rollingStyle value="Composite" />
 ...
 ...

listed works but I did not have to use

<staticLogFileName value="false" /> 

. I think the RollingAppender must (logically) ignore that setting since by definition the file gets rebuilt each day when the application restarts/reused. Perhaps it does matter for immediate rollover EVERY time the application starts.

查看更多
虎瘦雄心在
3楼-- · 2019-01-04 19:29

I've tried all the answers, but there was always something missing and not functioning as expected for me.

Then I experimented a bit with the hints given in each answer and was successful with the following setting:

<appender name="RollingActivityLog" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="C:\temp\LOG4NET_Sample_Activity.log" />
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <staticLogFileName value="false" />
  <preserveLogFileNameExtension value="true" />
  <datePattern value="-yyyyMMdd" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level - %message%newline" />
  </layout>
</appender>

The issue with other combinations of parameters was that the latest file didn't have the time pattern, or that the time pattern was appended as .log20171215 which created a new file time (and file extension = new file type!) each day - or both issues appeared.

Now with this setting you are getting files like this one:

LOG4NET_Sample_Activity-20171215.log

which is what I wanted. To summarize:

  • Don't put the date pattern in the <file value=... attribute, just define it in the datePattern.

  • Make sure you have preserveLogFileNameExtension's value attribute set to true.

  • Set the rollingStyle's attribute value to Date.

查看更多
贪生不怕死
4楼-- · 2019-01-04 19:32
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  <file value="logs\" />
  <datePattern value="dd.MM.yyyy'.log'" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="5MB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
</appender>
查看更多
趁早两清
5楼-- · 2019-01-04 19:37

In your Log4net config file, use the following parameter with the RollingFileAppender:

<param name="DatePattern" value="dd.MM.yyyy'.log'" />
查看更多
【Aperson】
6楼-- · 2019-01-04 19:38

For a RollingLogFileAppender you also need these elements and values:

<rollingStyle value="Date" />
<staticLogFileName value="false" />
查看更多
【Aperson】
7楼-- · 2019-01-04 19:41

I ended up using (note the '.log' filename and the single quotes around 'myfilename_'):

  <rollingStyle value="Date" />
  <datePattern value="'myfilename_'yyyy-MM-dd"/>
  <preserveLogFileNameExtension value="true" />
  <staticLogFileName value="false" />
  <file type="log4net.Util.PatternString" value="c:\\Logs\\.log" />

This gives me:

myfilename_2015-09-22.log
myfilename_2015-09-23.log
.
.
查看更多
登录 后发表回答