views:

125

answers:

2

How to "overwrite existing log file" ??? I have tried to set attribute property <appendToFile value="false" />. but still log file is not getting overwrite. My config file setting is as follow:

  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <file value="Content/log.txt" />
      <appendToFile value="false" />
      <threshold value="ON" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
      </layout>
    </appender>
  </log4net>

please let me know where i am doing mistake.

A: 

Is overwriting the existing file means you want to keep writing to the same file (like a circular buffer)? If so,

1) You should set maxSizeRollBackups=0.

2) You should also set appendToFile = true

Please try and let us know of your results

Syd
hey Syd thanks for your kind interest. I have added the attributes as per told by u but the log information is still getting appended in file.i want to overwrite the data. Please let me know if i am doing anything wrong.
Amit
@Amit, can you clarify your requirement one more time? Do you want the file to be overwritten instead of being appended to everytime you start your logging process?
Syd
My bad. The second statement is incorrect: should set appendToFile=false. Since this is still not working, let me check my logfiles when I go to work tomorrow. Silly question, but have you restarted IIS?
Syd
A: 

I think you might want to use the FileAppender rather than the RollingFileAppender. It allows you to overwrite the existing file.

http://logging.apache.org/log4net/release/sdk/log4net.Appender.FileAppender.html

<appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="Content/log.txt" />
    <appendToFile value="false" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

Set appendToFile to false

The following shows how to write the rollingapender file once per execution, but it will still create a new file per execution:

From the examples page

<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Content/log.txt" />
    <appendToFile value="false" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="-1" />
    <maximumFileSize value="10MB" />
    <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
    </layout>
</appender>
Daniel Dyson
hey Daniel Dyson ,,, thanks a lots for your reply... but still its not working for me. I have added the same code given by u in my config.. but instead of overwriting the log file its start creating multiple file in Content folder.i want override the information in same file.Please give ur view. Thanks
Amit
hey Daniel ,,, sorry my mistake... its working fine with FileAppender as per referred by u.Thanks a lots.
Amit