Log4Net and DebugAppender

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.
  1. <log4net>  
  2.   <root>  
  3.     <level value="ALL" />  
  4.     <appender-ref ref="DebugAppender"/>  
  5.   </root>  
  6.   <appender name="DebugAppender" type="log4net.Appender.DebugAppender">  
  7.     <layout type="log4net.Layout.PatternLayout">  
  8.       <conversionPattern value="%date{dd.MM.yyyy HH:mm:ss.ffff} [%thread] %level %logger%exception - %message%newline" />  
  9.     </layout>  
  10.   </appender>  
  11. </log4net>  
Next, let's hook up the file the XSD schema.
  1. While in the log4net.config file, click on XML across the top of the screen,
  2.  Select Schemas,
  3. 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.
  1. <configSections>  
  2.   <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>  
  3. </configSections>  
  4. <log4net configSource="log4net.config"/>  
  5. <startup>  
  6.   <supportedRuntime version="v4.0" sku=".NETFramework, Version=v4.5"/>  
  7. </startup>  
Finally, in the AssemblyInfo.cs file you need to add the following line at the bottom of the document.
  1. // Version information for an assembly consists of the following four values:  
  2. //  
  3. //      Major Version  
  4. //      Minor Version  
  5. //      Build Number  
  6. //      Revision  
  7. //  
  8. // You can specify all the values or you can default the Revision and Build Numbers  
  9. // by using the '*' as shown below:  
  10. [assembly: AssemblyVersion("1.0.0.0")]  
  11. [assembly: AssemblyFileVersion("1.0.0.0")]  
  12. [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config")]  
Now, your project is configured for log4net so let's use it to create a log message.
 
In your HomeController, add the following code: 
  1. private static readonly log4net.ILog log = log4net.LogManager.GetLogger("DebugAppender");  
  2.   
  3. public ActionResult Index()  
  4. {  
  5.     log.Info("Hello world!");  
  6.     return View();  
  7. }  
When you run your project, the logging line above will render values in the Output window as shown below.
 
 
Of course, there is a world of other possibilities in terms of logging.  The destination of the messages (file, console, database, etc.) as well as the logging levels (debug, info, warning, error, critical, etc.) are all configurable, but that's for a different blog.
 
Log4net is certainly powerful and a valuable tool for any application. 
 
Enjoy!