The venerable log4net
is the Apache Foundation solution for a .NET logger based on a java predecessor, log4j
.
It is also installable via NuGet but does require some non-trivial configuration due to its power and flexibility. log4net
can be configured in code or in the project configuration files (app.config
or web.config
).
Logs can be stored in flat files or in a database system of your choosing, or both, via the extensive set of available appenders
. For example, the RollingFileAppender
writes to a file which "rolls over" (starts a new file) at a specified size or interval:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
Multiple appenders can be used concurrently to direct log entries to multiple destinations. Also, multiple loggers can be defined, each with their own set of appenders.
The full feature set is listed here.