Log4net - create a new log file each time the application starts

Can I create separate log files each time the application starts? If I run my application 2 times, I should get 2 separate log files, I hope the file names can be added with the created dateTime

eg:
log_0830 - when the application starts at 8:30 a.m.

log_2130 - when the application starts at 9:30 pm

+6
source share
2 answers

I believe that you can do this in the configuration as follows:

<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> 
+3
source

Put this in 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> 

This configuration will create the file names:
log_0830.txt - when the application starts at 8:30 a.m.
log_2130.txt - when the application starts at 9:30 pm

+2
source

Source: https://habr.com/ru/post/890389/


All Articles