I am using Log4net for a while now and it's an amazing logging framework, especially when hooked into Castle.Windsor. However...
I usually use the rolling file appender, but this has resulted in too many log files than I actually want, so instead, for my latest project, have used the basic LogFileAppender instead, but the problem is the log file keeps growing (seemingly forever).
How can I tell the appender to not go over a fixed size (and start removing old logs and appending the new ones to the file?
My current configuration looks like:
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="E:\Logs\iWater\Schedule-Dispatch-API.log"/>
<param name="AppendToFile" value="true"/>
<maximumFileSize value="2048KB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-16date{dd MMM HH:mm:ss} %-7level %-25.35logger{1} %message%newline"/>
</layout>
</appender>
Its seems like the maximumFileSize attribute is not being respected. Any solutions?
Alternatively, how can I configure the rolling file appender to only create 1 file (ever)?
RollingFileAppender means the system creates a log file based on your filters, this way you can have log files based on dates (one file each day), or get the file splitted into small chunks when it hits certain size.
Gets or sets the maximum number of backup files that are kept before the oldest is erased.
In your case, the log file will be in bin\Debug\netcoreapp3.
log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.
The FileAppender class does not have the MaxFileSize/MaximumFileSize properties. You only get those if you use a RollingFileAppender. Here's an example that will limit your file to a fixed maximum size, with no backups (set maxSizeRollBackups to 0). Note that when the file reaches its max size, it truncates (basically deletes all existing logging and starts over):
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log.txt" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="0" /> <maximumFileSize value="10MB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender>
The LogFileAppender does not support limiting the output file size (at least in the references I can find). To limit the file size, use a RollingFileAppender and roll on Size and set the file size limit.
To limit the number of roll over files use the MaxSizeRollBackups attribute
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