Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

log4net one file per run

I need my application to create a log file each time it runs.

My preferred format would be App.log.yyyy-MM-dd_HH-mm-ss. If that's not possible, I'd settle for App.log.yyyy-MM-dd.counter

This is my current appender configuration:

<appender name="File" type="log4net.Appender.RollingFileAppender">
  <file value="App.log"/>
  <rollingStyle value="Date"/>
  <datePattern value=".yyyy-MM-dd_HH-mm-ss"/>
  <staticLogFileName value="false"/>
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>

But it creates a random number of files based on the date and time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume that the application should create only one log file every time it runs, so you do not need a rolling file appender (though my solution would apply for rolling file appenders as well):

<appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file type="log4net.Util.PatternString" value="c:empApp-%date{yyyy-MM-dd_HH-mm-ss}.log" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%2thread] %-5level - %message%newline" />
    </layout>
</appender>

(Obviously you can use other your own layout and other settings for the file appender.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...