开发者

Log4net - Create new log file each time application is started

开发者 https://www.devze.com 2023-03-12 17:34 出处:网络
Can we create separate log files each time when t开发者_开发百科he application is run? If i run my application 2 times, I should get 2 separate log files, hopefully the file names can be appended with

Can we create separate log files each time when t开发者_开发百科he application is run? If i run my application 2 times, I should get 2 separate log files, hopefully the file names can be appended with the created dateTime

eg:

log_0830 - when application is run on 8:30 am

log_2130 - when application is run on 9:30 pm


Put that into your app.config:

 <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
 </configSections>
 <log4net>
    <appender name="file" type="log4net.Appender.RollingFileAppender">
      <file value="log_"/>
      <rollingStyle value="Date"/>
      <datePattern value="HHmm.\tx\t"/>
      <staticLogFileName value="false"/>
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>      
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="file"/>
    </root>
  </log4net>

That configuration would produce filenames:
log_0830.txt - when application is run on 8:30 am
log_2130.txt - when application is run on 9:30 pm


I believe you could do this in the configuration like so:

<appender name="LogFileAppender" type="log4net.Appender.FileAppender"> 
    <file type="log4net.Util.PatternString" value="log-file-%d [%t].txt" /> 
    <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" /> 
</appender> 
0

精彩评论

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