How to set up and configure error logging in .NET with log4net

Posted written by Paul Seal on May 17, 2016 .NET Framework

This is a simple and straight forward tutorial on how to set up and configure error logging with log4net in your .NET application.

Install log4net

Install the log4net package using NuGet Package Manager

PM> Install-Package log4net

In the AssemblyInfo.cs file, add the following line: (This is in the properties folder of your application usually)

[assembly: log4net.Config.XmlConfigurator(Watch=true)]

Now log4net will know to look in the application's config file for its configuration settings.

Configuring the application

The next thing to do is define a config section for log4net in the web.config or app.config file at the root of the site/application.

<configuration>
   <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
   </configSections>

   <!-- other configurations settings here -->

</configuration>

Now you can add your log4net settings int the log4net config section

I have given you 3 example settings to use:

 

1. Basic settings. Console output only

<log4net>
   <root>
      <level value="ALL" />
      <appender-ref ref="ConsoleAppender" />
   </root>
   <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="%date %level %logger - %message%newline" />
      </layout>
   </appender>
</log4net>

 

2. Console output and writing error logs to a file

This will create the log file in a Logs folder in the App_Data folder.

 <log4net>
    <root>
       <level value="ALL" />
       <appender-ref ref="ConsoleAppender" />
       <appender-ref ref="FileAppender" />
    </root>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
       <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date %level %logger - %message%newline" />
       </layout>
    </appender>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
       <file type="log4net.Util.PatternString" value="App_Data\Logs\TraceLog.%property{log4net:HostName}.txt" />
       <appendToFile value="true" />
       <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
       <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date %level %logger - %message%newline" />
       </layout>
    </appender>
 </log4net>

 

3. Console output and writing logs to a different file each day

This will also create the log files in a Logs folder in the App_Data folder.

 <log4net>
    <root>
       <level value="ALL" />
       <appender-ref ref="ConsoleAppender" />
       <appender-ref ref="RollingFileAppender" />
    </root>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
       <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date %level %logger - %message%newline" />
       </layout>
    </appender>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
       <file type="log4net.Util.PatternString" value="App_Data\Logs\TraceLog.%property{log4net:HostName}.txt" />
       <appendToFile value="true" />
       <rollingStyle value="Date" />
       <maxSizeRollBackups value="5" />
       <maximumFileSize value="5MB" />
       <staticLogFileName value="true" />
       <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
       </layout>
    </appender>
 </log4net>



 

Using log4net in your code

using log4net;
using System.Reflection;

ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
Log.Error("Testing log4net error logging", ex); 

 

I hope you found this useful. Feel free to share with your friends or colleagues using the share icons.