问题
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