Log4net has quickly become the de facto logging package for many C# and VB web development packages. You'd think, because of that, the setup and configuration would be seamless. Install and BOOM! You're up and running. It's not that easy, however.
In this blog we will install and configure log4net for a MVC project.
Start with installing the package. Within Visual Studio, click Tools, NuGet Package Manager, and Manage NuGet Pacakges for Solution. Click on Browse and search for log4net. At this time, this is the latest package.
Install it to your MVC application (and whatever other projects might use it). Close out of the package manager.
Now, add the configuration file. In the web project, add a file called Log4Net.config and paste the following into the file.
- <log4net>
- <root>
- <level value="ALL" />
- <appender-ref ref="DebugAppender"/>
- </root>
- <appender name="DebugAppender" type="log4net.Appender.DebugAppender">
- <layout type="log4net.Layout.PatternLayout">
- <conversionPattern value="%date{dd.MM.yyyy HH:mm:ss.ffff} [%thread] %level %logger%exception - %message%newline" />
- </layout>
- </appender>
- </log4net>
Next, let's hook up the file the XSD schema.
- While in the log4net.config file, click on XML across the top of the screen,
- Select Schemas,
- Find DotNetConfig.XSD and click the Use checkbox in column 1 as shown below.

Click Ok to save your changes. All this does is avoid a compiler warning, but it is certainly valuable in configuration.
Next, make sure the log4net.config file is always copied to the output folder. Right click on log4net.config file and then select Properties. Change the Copy to Output Directory to Copy Always as shown below.
Next thing you need to do is setup your web.config file to use the log4net configuration file and configure the section. To do that, open your web.config and paste in the following highlighted lines. These lines are added directly below the <configuration> tag.

Join the conversation! Your thoughts help the community grow.