开发者

log4net - creating only one logfile

开发者 https://www.devze.com 2023-04-02 06:20 出处:网络
I am using log4net and when I\'m running my application, several logfiles are created. It appears the log4net is creating a new logfile whenever the time-format changes, meaning I end up with a new l

I am using log4net and when I'm running my application, several logfiles are created. It appears the log4net is creating a new logfile whenever the time-format changes, meaning I end up with a new logfile whenever a minute has passed.

I have probably missunderstod the xml syntax used, but I'm not sure where its wrong (maybe RollingFileAppender?).

Here's the xml file:

<log4net>

  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %-5level [%thread] %logger:%line - %message%newline"/>
    </layout>
  </appender>

  <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
    <appendToFile va开发者_运维百科lue="true"/>
    <rollingStyle value="Date"/>
    <staticLogFileName value="false" />
    <datePattern value="yyyyMMdd_hhmm'.log'"/>
    <file value="Boghe"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %-5level [%thread] %logger:%line - %message%newline"/>
    </layout>
  </appender>

  <root>
    <level value="ALL"/>
    <appender-ref ref="ConsoleAppender"/>
    <appender-ref ref="FileAppender"/>
  </root>

</log4net>

Thanks


If you really want just one file then you should use the normal file appender:

http://logging.apache.org/log4net/release/config-examples.html#FileAppender

Based on your comments I assume your application runs for a "short" time and therefore you want to have some date / time information in your log file name. This you can do like this:

<file type="log4net.Util.PatternString" value="Boghe%date{yyyyMMdd_hhmm}.log" />

Update:

Configuration Sample (that works for me):

<appender name="FileAppender" type="log4net.Appender.FileAppender">
   <file type="log4net.Util.PatternString"
         value="c:\temp\Boghe%date{yyyyMMdd_hhmm}.log" />

   <appendToFile value="true" />
   <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date - %message%newline" />
   </layout>
</appender>


You need to change your date pattern to something like

<datePattern value="yyyyMMdd" />

This will roll your logfile once a day.


Example: With this configuration

<file value="Foo" />
<StaticLogFileName value="false">
<datePattern value="yyyyMMdd_hhmm"/>

you are telling the file appender to create a new logfile every minute (if you are writing something to the logs at least once a minute).

Say you started your application at 17:00:00 on 09/21/2011. You output logging information every second.
This means that for the first 60 seconds (until 17:00:59) all output will be into a file named Foo20110921_1700.
At 17:01:00 the next output occurs and the file will be 'rolled over', meaning a new file Foo20110921_1701 is created.
For the next 60 seconds all output will be into this new file. The same will happen again at 17:02:00, 17:03:00 and so forth.


See RollingFileAppender documentation for more information


Alternatively, if you don't want to roll by date, use the Size value

    <rollingStyle value="Size"/>
    <maximumFileSize value="10MB"/>

So you will roll your files every 10MB.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号