Here’s a short tutorial on how to use log4net in C#

1. Get log4net from the apache website or use NuGet to include in your project it


2. Add the Appender section to your app.config. The following code uses a file for the logging:

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />

3. Use the following code to use the configuration you just added to app.config:

static void Main()

4. To log use the following code:

using log4net;

private static readonly ILog log = LogManager.GetLogger(typeof(YourClassOrApp));
log.Debug("this is the first log message");

By Enrico

My greatest passion is technology. I am interested in multiple fields and I have a lot of experience in software design and development. I started professional development when I was 6 years. Today I am a strong full-stack .NET developer (C#, Xamarin, Azure)

This site uses Akismet to reduce spam. Learn how your comment data is processed.