Currently my application is using log4net to log errors, the web.config for this is as followed:
<log4net> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="c:/paypal/logs/gateway.log" /> <appendToFile value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="FileAppender" /> </root> </log4net>
However, the problem with this is that everyday the logs always go to gateway.log
Does anybody know a way to have a different log file per day using log4net?
Ideally I don't want to have to manually edit the web.config
every night using a scheduled task.
You should use RollingFileAppender with rollingStyle Date
and datePattern yyyyMMdd
(this will roll every day).
Example of configuration:
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender"> <file value="c:/paypal/logs/gateway.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" /> </layout> </appender>
UPDATE: I think a better way is to include the date into file name. And use datePattern value only to show when new log file should be created.
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="c:/paypal/logs/gateway_%date{yyyyMMdd}.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" /> </layout> </appender>
In this case all your files will have name like gateway_20120427.log
.
Like using the RollingFileAppender
<appender name="RollingLogFileAppenderOutput" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="c:\my.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd'.log'" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level - %message%newline" /> </layout> </appender>
edit
This is the whole log4net configuration section to roll everyday, the same as @lazyberezovsky answered just adding the log4net and root for clarification
edit2 <file value="c:/paypal/logs/gateway_" />
this will create a file named 'gateway_' and at the end of the day (2012-04-27) it will be renamed as gateway_20120427.log and the next day (2012-04-28) it will create again the file gateway_ and at the end it'll create gateway_20120428.log.
<log4net> <appender name="RollingLogFileAppenderOutput" type="log4net.Appender.RollingFileAppender"> <file value="c:/paypal/logs/gateway_" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd'.log'" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level - %message%newline" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="RollingLogFileAppenderOutput" /> </root> </log4net>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With