how to create a new log file every time when the application starts using lo4net

*爱你&永不变心* 提交于 2019-12-12 01:57:54

问题


how to create a new log file every time when the application starts using lo4net. or I wanted to clear the log file each time. RollingFileAdapter doesn't give me any solution. see my code:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <param name="File" value="Log.txt"/>
  <param name="AppendToFile" value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="1MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%identity - [%date] [%level] %class %method  %message %exception %newline" />
  </layout>
</appender>

here I have to add anything or anything else?

Now I changed to this but I want a filename added with datetime for each execution.

<appender name="RollingFileAppender"type="log4net.Appender.RollingFileAppender"> 
  <file value="log\" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  <datePattern value="dd.MM.yyyy'.log'" />
  <appendToFile value="false" />
  <staticLogFileName value="false" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="5MB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="[%date] [%level] Class Name: %class Method Name: %method line No: %line %message %exception %newline" />
  </layout>

回答1:


You can make a new log file on each execution: rollingStyle -> once, and set appendtofile to false:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <param name="File" value="Log.txt"/>
  <param name="AppendToFile" value="false" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="1MB" />
  <staticLogFileName value="true" />
   <rollingStyle value="Once" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%identity - [%date] [%level] %class %method  %message %exception %newline" />
  </layout>
</appender>


来源:https://stackoverflow.com/questions/39526161/how-to-create-a-new-log-file-every-time-when-the-application-starts-using-lo4net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!