Question

I'm using log4net in a c# windows service, so the main application runs every time. I configured log4net to log into a date specific directory. Inside this directory log4net creates a logfile with a timestamp in the filename. This works fine. The probem is, that it only creates a new logfile, when the windows service restarts. This causes the logfile to get very big, expecially if the service runs over several days.

The question is:

Is there a possibility to force log4net to start with a new logfile throughout my code.

This is my log4net config:

<log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file type="log4net.Util.PatternString" value="${ProgramData}\Sirona\Log\AcqSrv\%date{yyMMdd}\AcqSrvAll-%date{HHmmss}.log" />
        <appendToFile value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%2thread] [%-5level] [%logger] - %message%newline" />
        </layout>
    </appender>
    <root>
        <level value="ALL" />
        <appender-ref ref="FileAppender" />
    </root>     <!-- Do not want NHibernate log -->     
</log4net>

Best regards

Emu

Was it helpful?

Solution

I think your problem is solved by the RollingFileAppender: http://logging.apache.org/log4net/release/config-examples.html http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.html

It works exactly as a FileAppender but once it reach a maximum size (configurable) it creates another log file. You can also instruct it to keep only the latest X log files.

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top