本文介绍了每天创建一个文件夹,当达到最大大小时创建一个新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在应用程序位置之外创建一个日志目录,并为每个日期创建一个文件夹.当达到最大大小时,我还需要创建一个新文件.
I need to create a logs directory outside the location of my application and a folder for each date. I also need to create a new file when the max size is reached.
我正在使用以下代码:
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<file type="log4net.Util.PatternString" value="D:/Logs/EmailService/"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<datePattern value=".yyyyMMdd\\\\'service.log"/>
<staticLogFileName value="true"/>
<maximumFileSize value="5MB"/>
<maxSizeRollBackups value="20"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] – %message%newline"/>
</layout>
<lockingModel type="log4net.Appender.FileAppender+MutexLock" />
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
但是,没有创建文件.怎么了?
However, the files are not being created. What is wrong with it?
这是完整的代码,它与@stuartd的答案一起可以在每个日期创建目录:
Here's the complete code, that along with @stuartd's answer were able to create the directory per date:
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<file type="log4net.Util.PatternString" value="D:/Logs/emailservice/%date{yyyyMMdd}/logfile.log"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<maximumFileSize value="5MB"/>
<maxSizeRollBackups value="20"/>
<datePattern value="yyyyMMdd"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] – %message%newline"/>
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="WARN" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
推荐答案
为了滚动日期和大小,您必须在配置中设置<rollingStyle value="Composite"/>
-来自文档:
in order to roll on date and size you must set <rollingStyle value="Composite"/>
in your config - from the docs:
这篇关于每天创建一个文件夹,当达到最大大小时创建一个新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!